Mybatis
1.依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
2.配置
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/xz_test?serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false&useSSL=false
username: root
password: 123456
initialSize: 10
maxActive: 20
minIdle: 1
maxWait: 60000
mybatis:
mapper-locations: classpath:mappers/*.xml
3.代码示例
mapper的扫描只用@mapperScan注解就行, 至于idea编译报错, 装个mybatis的插件就行, 参照:
https://www.imooc.com/article/287865
---------------dao
public interface UserMapper {
List<User> listAll();
}
---------------mapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.xinzhang0618.springboot.demo.UserMapper">
<resultMap id="resultMap" type="top.xinzhang0618.springboot.demo.User">
<id column="user_id" property="userId"/>
<result column="created_time" property="createdTime"/>
<result column="department_id" property="departmentId"/>
<result column="department_name" property="departmentName"/>
<result column="email" property="email"/>
<result column="head_url" property="headUrl"/>
<result column="is_enable" property="enable"/>
<result column="is_system" property="system"/>
<result column="login_name" property="loginName"/>
<result column="login_password" property="loginPassword"/>
<result column="mobile" property="mobile"/>
<result column="modified_time" property="modifiedTime"/>
<result column="nickname" property="nickname"/>
<result column="remark" property="remark"/>
<result column="telephone" property="telephone"/>
<result column="user_name" property="userName"/>
</resultMap>
<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>
<select id="listAll" resultMap="resultMap">
select
<include refid="columns"/>
from oms_user
</select>
</mapper>
---------------启动类
@SpringBootApplication
@MapperScan("top.xinzhang0618.springboot.demo")
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
---------------测试
@Autowired
private UserMapper userMapper;
@Test
public void test() {
List<User> list = userMapper.listAll();
System.out.println(list);
}