一、引入
<!--mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
二、配置
server:
port: 8081
spring:
#数据库连接配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://47.107.105.158:3306/test?characterEncoding=utf-8&useSSL=false
username: root
password: 123456
#mybatis的相关配置
mybatis:
#mapper配置文件
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.demo.mybatis.model
#开启驼峰命名
configuration:
map-underscore-to-camel-case: true
三、实体类
- 位置在
type-aliases-package
配置的包中
public class User implements Serializable {
private Long id;
private String username;
private String password;
//...get set方法
}
四、DAO层
@Mapper
public interface UserMapper {
List<User> findAll();
}
- 在
mapper-locations
中配置的位置编写和接口对应的映射表
UserMapper.xml
<?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="com.demo.mybatis.mapper.UserMapper">
<select id="findAll" resultType="User">
SELECT * FROM USER
</select>
</mapper>
五、使用
// 注入即可直接使用
@Autowired
private UserMapper userMapper;
网友评论