为了更上技术的革新,近期打算学习下spring-Boot跟Spring-Cloud。下面将记录下自己学习过程。Spring-boot系列学习源代码放在https://github.com/wenbo2018/spring-boot-learning,有需要的可以自行下载。
spring-boot整合mybatis是非常方便而且简单的,没有以往那么多配置文件,下面记录详细过程。
1.maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<!-- Spring Boot web 依赖 -->
<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>1.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2.application.properties配置文件
配置数据源和mybatis
#dataSource
spring.datasource.url=jdbc:mysql://localhost:3306/spring
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.profiles.active=dev
## Mybatis 配置
#实体类包
mybatis.typeAliasesPackage=com.github.wenbo2018.dto
#xml映射文件所在位置,通常weichatresources/mapper/
mybatis.mapperLocations=classpath:mapper/*.xml
3.mybatis配置
UserDao.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.github.wenbo2018.dao.UserDao">
<resultMap id="BaseResultMap" type="com.github.wenbo2018.dto.User">
<result column="Id" property="id" jdbcType="INTEGER" />
<result column="UserName" property="username" jdbcType="VARCHAR" />
<result column="PassWord" property="password" jdbcType="VARCHAR" />
<result column="Email" property="email" jdbcType="VARCHAR" />
<result column="Au" property="au" jdbcType="INTEGER" />
</resultMap>
<insert id="add" parameterType="com.github.wenbo2018.dto.User" >
insert into User (
UserName,
PassWord,
Au,Email,Status
)
values (
#{username},
#{password},
#{au},#{email},#{status}
)
</insert>
<select id="list" resultMap="BaseResultMap">
SELECT * FROM User
</select>
<delete id="delete" parameterType="int">
DELETE FROM User WHERE Id=#{id}
</delete>
<select id="load" parameterType="int" resultMap="BaseResultMap">
SELECT * FROM User WHERE Id=#{id}
</select>
<update id="update" parameterType="com.github.wenbo2018.dto.User">
UPDATE User SET UserName=#{username}, Password=#{password},Au=#{au},Email=#{email}
</update>
<select id="queryByUserNameAndPassWord" resultMap="BaseResultMap">
SELECT * FROM User WHERE UserName=#{0} and PassWord=#{1}
</select>
<select id="loadByUserName" parameterType="java.lang.String" resultMap="BaseResultMap">
SELECT * FROM User WHERE UserName=#{username}
</select>
</mapper>
public interface UserDao {
public void add(User user);
public User load(int userId);
public User loadByUserName(String username);
public void delete(int userId);
public void update(User user);
List<User> list();
User queryByUserNameAndPassWord(String username, String password);
}
实体类User
package com.github.wenbo2018.dto;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
/**
* Created by shenwenbo on 2017/4/14.
*/
@Getter
@Setter
public class User implements Serializable {
private static final long serialVersionUID = -7091478494529379221L;
private int id;
private String username;
private String password;
private int au;
private String email;
private int status;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
@Getter @Setter可以自动构造get set方法,只需要依赖lombok即可。
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.12.6</version>
</dependency>
Application.class配置
@SpringBootApplication
@MapperScan("com.github.wenbo2018.dao")
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(Application.class);
app.run(args);
}
}
@MapperScan(“com.github.wenbo2018.dao”)用户扫描mapper接口,com.github.wenbo2018.dao为mapper接口所在包,spring-boot会自动将其注入到ioc中。