博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis环境搭建(eclipse,idea)
阅读量:4363 次
发布时间:2019-06-07

本文共 4942 字,大约阅读时间需要 16 分钟。

基于java配置SSM,eclipse

新建maven,web项目

....

项目结构:

1250855-20190819163332846-1746435826.png

jar包

spring和DispatcherServlet上下文

public class DemoWebApplicationInitializer    extends AbstractAnnotationConfigDispatcherServletInitializer  {    @Override    protected Class
[] getRootConfigClasses() { // TODO Auto-generated method stub return new Class
[] {RootConfig.class}; } @Override protected Class
[] getServletConfigClasses() { // TODO Auto-generated method stub return new Class
[] {WebConfig.class}; } @Override protected String[] getServletMappings() { // TODO Auto-generated method stub return new String[] {"/"}; } }

DispatcherServlet

@Configuration  @EnableWebMvc  @ComponentScan(basePackages = {"com.getword.controller"})  public class WebConfig extends WebMvcConfigurerAdapter {    @Bean    public ViewResolver viewResolver() {        InternalResourceViewResolver resolver = new InternalResourceViewResolver();        resolver.setPrefix("/WEB-INF/views/");        resolver.setSuffix(".jsp");        resolver.setExposeContextBeansAsAttributes(true);        return resolver;    }    /**     * 静态资源     */    @Override    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {        // TODO Auto-generated method stub        configurer.enable();    }  }

spring 上下文

@Configuration  @ComponentScan(basePackages = {"com.getword"},        excludeFilters = {@Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)})  @Import(DataSourceConfig.class)  public class RootConfig {    @Bean    public BeanNameAutoProxyCreator autoProxyCreator() {        BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();        autoProxyCreator.setProxyTargetClass(true);        // 设置要创建代理的那些Bean的名字        autoProxyCreator.setBeanNames("*Service");        autoProxyCreator.setInterceptorNames("transactionInterceptor");        return autoProxyCreator;    }  }

DataSourceConfig

@Configuration  @MapperScan("com.getword.dao")  public class DataSourceConfig {    private final static Logger LOG = Logger.getLogger(DataSourceConfig.class);    private String driver = "com.mysql.jdbc.Driver";;    private String url = "jdbc:mysql://localhost:3306/spring?characterEncoding=UTF-8&serverTimezone=UTC";    private String username = "root";    private String password = "123";    @Bean    public BasicDataSource dataSource() {        LOG.info("Initialize the BasicDataSource...");        BasicDataSource dataSource = new BasicDataSource();        dataSource.setDriverClassName(driver);        dataSource.setUrl(url);        dataSource.setUsername(username);        dataSource.setPassword(password);        return dataSource;    }    // mybatis的配置    @Bean    public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException {        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();        sqlSessionFactoryBean.setDataSource(dataSource());        sqlSessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources("classpath*:mappers/*.xml"));        sqlSessionFactoryBean.setTypeAliasesPackage("com.getword.domain");// 别名,让*Mpper.xml实体类映射可以不加上具体包名        return sqlSessionFactoryBean;    }      // 事务管理器 对mybatis操作数据库事务控制,spring使用jdbc的事务控制类    @Bean(name = "transactionManager")    public DataSourceTransactionManager dataSourceTransactionManager() {        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();        dataSourceTransactionManager.setDataSource(dataSource());        return dataSourceTransactionManager;    }      @Bean(name = "transactionInterceptor")    public TransactionInterceptor interceptor() {        TransactionInterceptor interceptor = new TransactionInterceptor();        interceptor.setTransactionManager(dataSourceTransactionManager());        Properties transactionAttributes = new Properties();        transactionAttributes.setProperty("save*", "PROPAGATION_REQUIRED");        transactionAttributes.setProperty("del*", "PROPAGATION_REQUIRED");        transactionAttributes.setProperty("update*", "PROPAGATION_REQUIRED");        transactionAttributes.setProperty("get*", "PROPAGATION_REQUIRED,readOnly");        transactionAttributes.setProperty("find*", "PROPAGATION_REQUIRED,readOnly");        transactionAttributes.setProperty("*", "PROPAGATION_REQUIRED");        interceptor.setTransactionAttributes(transactionAttributes);        return interceptor;    }  }

注意:mapper的命名空间必须和对应的接口的全路径一致!!!

idea,从maven简单java项目转web项目

新建maven项目

  1. 新建maven项目

1250855-20190819163400847-2092553825.png

  1. 填写group id和artifictId,next

1250855-20190819163522392-1008079704.png

  1. 输入项目名称,finish
  2. 配置maven,次步骤最后在新建项目之前
  3. 1250855-20190819163722381-1923061362.png

项目结果如下:

1250855-20190819163817287-875344901.png

添加web模块

  1. 项目结构->Modules->add->web

1250855-20190819164610682-908751136.png

  1. 删除web.xml

1250855-20190819164718982-598805534.png

  1. 设置web项目根路径

1250855-20190819164844317-1740600229.png

  • 添加artifact

1250855-20190819171643219-1833564188.png

  1. 配置Tomcat
  2. 此时项目结构

1250855-20190819171547498-1825423205.png

此时可以访问webapp下的静态文件了

  1. jar包,pom.xml

注意:使用maven时也要添加servlet依赖,注意作用域。此时可以使用servlet了

spring配置

同eclipse。

源码附件

转载于:https://www.cnblogs.com/zhuxiang1633/p/11356819.html

你可能感兴趣的文章
『重构--改善既有代码的设计』读书笔记----代码坏味道【4】
查看>>
Java开发者值得关注的7款新工具
查看>>
Spring Boot + Jersey
查看>>
Web前端学习的路径分享,前端学习方法及途径
查看>>
贪吃蛇小游戏
查看>>
USE PDFCREATE TO CREATE A PDF FILE
查看>>
使用nodejs开发前后端分离式接口+数据库访问
查看>>
关于Spring MVC写的不错的几篇博客
查看>>
第八章 watch监听 84 watch-监视路由地址的改变
查看>>
IDEA tomcat乱码
查看>>
个人作业3——个人总结(Alpha阶段)
查看>>
第十章—DOM(三)——Text类型
查看>>
supersocket实现上传文件
查看>>
python decimal和fractions模块
查看>>
Linux基本内容2
查看>>
Linux命令第二部分(用户和组操作)(共15个)
查看>>
MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]
查看>>
C++的多态与切片问题(Section Problem)
查看>>
python装饰器
查看>>
U盘传送容量与格式问题
查看>>