使用 Gradle 构建工具实现 Spring 集成 MyBatis (使用 Java 注解方式)

来源:互联网 发布:减肥变速跑和慢跑知乎 编辑:程序博客网 时间:2024/06/03 22:32

使用 Gradle 构建工具实现 Spring 集成 MyBatis (使用 Java 注解方式)

1、添加依赖

compile 'org.springframework:spring-core:4.1.7.RELEASE'compile 'org.springframework:spring-context:4.1.7.RELEASE'compile 'org.springframework:spring-jdbc:4.1.7.RELEASE'compile 'org.mybatis:mybatis:3.3.1'compile 'org.mybatis:mybatis-spring:1.2.5'compile 'commons-dbcp:commons-dbcp:1.4'compile 'mysql:mysql-connector-java:5.1.38'compile 'log4j:log4j:1.2.17'

2、配置数据源

/**  * 可以通过注入的方式注入进来  * @return  */ @Bean public DataSource dataSource(){     BasicDataSource dataSource = new BasicDataSource();     dataSource.setUrl("jdbc:mysql://localhost:3306/langying");     dataSource.setDriverClassName("com.mysql.jdbc.Driver");     dataSource.setUsername("root");     dataSource.setPassword("123456");     return dataSource; }

以上相当于我们原来配置的 xml 片段:

<!--本示例采用 DBCP 连接池,应预先把 DBCP 的 jar 包复制到工程的 lib 目录下。 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">    <property name="driverClassName" value="${jdbc.driverClassName}"/>    <property name="url" value="${jdbc.url}"/>    <property name="username" value="${jdbc.username}"/>    <property name="password" value="${jdbc.password}"/></bean>

3、配置 sqlSessionFactory

@Bean(name = "sqlSessionFactory")public SqlSessionFactory sqlSessionFactoryBean(){    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();    bean.setDataSource(dataSource());    bean.setTypeAliasesPackage("com.liwei.entity");    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();    Resource[] resources = null;    try {        resolver.getResources("classpath:com/liwei/mapper/*.xml");    } catch (IOException e) {        e.printStackTrace();    }    bean.setMapperLocations(resources);    SqlSessionFactory factory = null;    try {        factory = bean.getObject();    } catch (Exception e) {        e.printStackTrace();    }    return factory;}

与 XML 片段的比较:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    <property name="dataSource" ref="dataSource" />    <property name="mapperLocations" value="classpath:com/liwei/ssm/mapper/*Mapper.xml" />    <!-- 还可以在这里配置 typeAliases  --></bean>

4、配置 MapperScannerConfigurer

@Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){    MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();    mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");    mapperScannerConfigurer.setBasePackage("com.liwei.mapper");    return mapperScannerConfigurer;}

与 XML 片段的比较:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    <property name="basePackage" value="com.liwei.ssm.mapper" />    <!-- 最佳实践 它的功能跟 sqlSessionFactory 是一样的,只是它指定的是定义好的 SqlSessionFactory 对应的 bean 名称 -->    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /></bean>

4、启动 Spring 容器测试
在启动测试类之前,我们来看看我们用于配置的 Java 类有何不同,或者说我们应该关注这个 Java 类词成为配置类代替 XML 文件,是由于哪些关键注解的作用呢?

@Configuration@ComponentScan(basePackages = "com.liwei")

Spring 容器启动类,测试代码:

public static void main(String[] args) {    // ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");    AnnotationConfigApplicationContext ctx =            new AnnotationConfigApplicationContext(MyBatisConfig.class);    SchoolService schoolService =(SchoolService)ctx.getBean("schoolService");    SchoolBean schoolBean = schoolService.getSchoolById(1);    System.out.println(schoolBean.getMaterial());    System.out.println(schoolBean.getSchoolName());    System.out.println(schoolBean.getSchoolType());}

附加说明:如果我们想看到后台执行的 SQL 语句,我们只要简单配置一下 log4j 的配置文件就可以了。

# 最最简单的 log4j 配置log4j.rootLogger=debug,stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.SimpleLayout

参考资料:
(1)张开涛老师的博客:
【第十二章】零配置 之 12.4 基于Java类定义Bean配置元数据 ——跟我学spring3 - 开涛的博客(欢迎关注我的公众号[博客头像二维码]) - ITeye技术网站 http://jinnianshilongnian.iteye.com/blog/1463704
(2)偶尔记一下的博客:
Spring Boot 集成MyBatis - 偶尔记一下 - 博客频道 - CSDN.NET http://blog.csdn.net/isea533/article/details/50359390
说明:该博客讲解了很多关于 MyBatis 的知识。
(3)
DBCP连接池配置参数说明 - ..7a. - 博客频道 - CSDN.NET http://blog.csdn.net/fairyhawk/article/details/7565391

0 0
原创粉丝点击