[关闭]
@chenbinghua 2018-08-13T16:13:44.000000Z 字数 2631 阅读 790

SpringBoot(第篇):整合MyBatis

SpringBoot


项目springdemo1

前言

引入依赖

  1. <!--springboot-mybatis整合包-->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. <version>1.3.2</version>
  6. </dependency>
  7. <!--mysql-jdbc驱动-->
  8. <dependency>
  9. <groupId>mysql</groupId>
  10. <artifactId>mysql-connector-java</artifactId>
  11. <scope>runtime</scope>
  12. </dependency>
  13. <!--druid连接池-->
  14. <dependency>
  15. <groupId>com.alibaba</groupId>
  16. <artifactId>druid</artifactId>
  17. <version>1.0.29</version>
  18. </dependency>

application.properties 添加相关配置

  1. # 连接池配置
  2. spring.datasource.url=jdbc:mysql://localhost/jfinal_demo?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
  3. spring.datasource.password=root
  4. spring.datasource.username=root
  5. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  6. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

springboot会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中,对了你一切都不用管了,直接拿起来使用就行了。

以上的配置修改了springboot的默认数据库连接池为druid

创建User类

在Mysql中创建User表,包含id(BIGINT)、name(INT)、age(VARCHAR)字段。同时,创建映射对象User。

  1. @Getter@Setter@ToString
  2. public class User {
  3. private Long id;
  4. private String name;
  5. private Integer age;
  6. }

创建User映射的操作UserMapper,为了后续单元测试验证,实现插入和查询操作。

  1. @Mapper
  2. public interface UserMapper {
  3. @Select("SELECT * FROM USER WHERE NAME = #{name}")
  4. User findByName(@Param("name") String name);
  5. @Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
  6. int insert(@Param("name") String name, @Param("age") Integer age);
  7. }

单元测试

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class Springdemo1ApplicationTests {
  4. @Autowired
  5. private UserMapper userMapper;
  6. @Test
  7. @Rollback
  8. public void findByName() throws Exception {
  9. userMapper.insert("bbbc", 20);
  10. User u = userMapper.findByName("bbbc");
  11. System.out.println(u);
  12. }
  13. }

优化

UserMapper.java
去掉@Mapper注解
去掉@Select @Insert注解
最后变成

  1. public interface UserMapper {
  2. User findByName(@Param("name") String name);
  3. int insert(@Param("name") String name, @Param("age") Integer age);
  4. }

因为去掉@Mapper注解,所以Spring找不到该bean

  1. @SpringBootApplication
  2. @MapperScan("com.chen.springdemo1.mapper")
  3. public class Springdemo1Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Springdemo1Application.class, args);
  6. }
  7. }

在UserMapper.java同级目录下创建UserMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.chen.springdemo1.mapper.UserMapper">
  6. <select id="findUserById" parameterType="int" resultType="com.chen.springdemo1.domain.User">
  7. select * from user where id=#{id}
  8. </select>
  9. </mapper>

在pom.xml添加

  1. <!--在pom文件中的build标签中添加这段配置,让在src下的资源也能加载到classes目录-->
  2. <resources>
  3. <resource>
  4. <directory>src/main/java</directory>
  5. <includes>
  6. <include>**/*.xml</include>
  7. </includes>
  8. </resource>
  9. </resources>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注