1.maven 配置文件:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.配置bean:
public class Dept { private Integer id; private String departmentName; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } }
dept contoller 文件:
@Autowired
private DeptMapper deptMapper;
@RequestMapping("/findOne/{id}")
public Dept findOne(@PathVariable("id") Integer id){
return deptMapper.findOne(id);
}
@RequestMapping("/list")
public List<Dept> list() {
return deptMapper.list();
}
@RequestMapping("/save")
public int save(Dept dept) {
deptMapper.save(dept);
return dept.getId();
}
mappper 文件:
@Mapper public interface DeptMapper { Dept findOne(Integer id); List<Dept> list(); int save(Dept dept); }
配置一个基本的xml 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="com.tuling.mapper.DeptMapper">
<select id="findOne" parameterType="Integer" resultType="com.tuling.bean.Dept">
select * from department where id = #{id}
</select>
<select id="list" resultType="com.tuling.bean.Dept">
select * from department
</select>
<insert id="save" parameterType="com.tuling.bean.Dept" keyProperty="id" useGeneratedKeys="true">
insert into department(department_name) values(#{departmentName})
</insert>
</mapper>
5.最后配置myitbit 的配置文件:
spring: datasource: druid: username: root password: 123456 jdbcUrl: jdbc:mysql://47.104.128.12:3306/tuling-vip driverClassName: com.mysql.jdbc.Driver initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true filters: stat,wall #\u914D\u7F6E\u8FC7\u6EE4\u5668 maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 #\u914D\u7F6Emybaits mybatis: configuration: map-underscore-to-camel-case: true mapper-locations: classpath:/mybatis/mapper/*.xml