pom.xml文件
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<!-- mybatis 整合包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mysql 数据库连接包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
application.properties文件
#mysql数据库配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatis配置:
#mapper位置
mybatis.mapper-locations=/mybatis/*
#别名
mybatis.type-aliases-package=com.jerry.work.bean
#驼峰命名法
mybatis.configuration.mapUnderscoreToCamelCase=true
实体类User
public class User{
private Integer id;
private String name;
private Integer age;
public User() {
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
dao接口UserMapper
@Mapper
public interface UserMapper {
User findUser(int id);
}
具体sql实现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="***.***.UserMapper">
<select id="findUserInfoById" parameterType="int" resultType="user">
select * from user where id = #{id};
</select>
</mapper>
到这里就可以在controller或service中调用UserMapper来根据id获取user了。