Mybatis

1.依赖

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.mybatis.spring.boot</groupId>
  7. <artifactId>mybatis-spring-boot-starter</artifactId>
  8. <version>2.1.2</version>
  9. </dependency>

2.配置

  1. spring:
  2. datasource:
  3. driverClassName: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/xz_test?serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false&useSSL=false
  5. username: root
  6. password: 123456
  7. initialSize: 10
  8. maxActive: 20
  9. minIdle: 1
  10. maxWait: 60000
  11. mybatis:
  12. mapper-locations: classpath:mappers/*.xml

3.代码示例
mapper的扫描只用@mapperScan注解就行, 至于idea编译报错, 装个mybatis的插件就行, 参照:
https://www.imooc.com/article/287865

  1. ---------------dao
  2. public interface UserMapper {
  3. List<User> listAll();
  4. }
  5. ---------------mapper
  6. <?xml version="1.0" encoding="UTF-8"?>
  7. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  8. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  9. <mapper namespace="top.xinzhang0618.springboot.demo.UserMapper">
  10. <resultMap id="resultMap" type="top.xinzhang0618.springboot.demo.User">
  11. <id column="user_id" property="userId"/>
  12. <result column="created_time" property="createdTime"/>
  13. <result column="department_id" property="departmentId"/>
  14. <result column="department_name" property="departmentName"/>
  15. <result column="email" property="email"/>
  16. <result column="head_url" property="headUrl"/>
  17. <result column="is_enable" property="enable"/>
  18. <result column="is_system" property="system"/>
  19. <result column="login_name" property="loginName"/>
  20. <result column="login_password" property="loginPassword"/>
  21. <result column="mobile" property="mobile"/>
  22. <result column="modified_time" property="modifiedTime"/>
  23. <result column="nickname" property="nickname"/>
  24. <result column="remark" property="remark"/>
  25. <result column="telephone" property="telephone"/>
  26. <result column="user_name" property="userName"/>
  27. </resultMap>
  28. <sql id="columns">created_time,department_id,department_name,email,head_url,is_enable,is_system,login_name,login_password,mobile,modified_time,nickname,remark,telephone,user_id,user_name</sql>
  29. <select id="listAll" resultMap="resultMap">
  30. select
  31. <include refid="columns"/>
  32. from oms_user
  33. </select>
  34. </mapper>
  35. ---------------启动类
  36. @SpringBootApplication
  37. @MapperScan("top.xinzhang0618.springboot.demo")
  38. public class SpringbootDemoApplication {
  39. public static void main(String[] args) {
  40. SpringApplication.run(SpringbootDemoApplication.class, args);
  41. }
  42. }
  43. ---------------测试
  44. @Autowired
  45. private UserMapper userMapper;
  46. @Test
  47. public void test() {
  48. List<User> list = userMapper.listAll();
  49. System.out.println(list);
  50. }