1、目录结构
2、pom.xml添加依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
3、在resouces下面添加mybatis文件夹,并添加mybatis-config.xml的配置文件,内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration >
<!-- 全局配置 -->
<settings>
<!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值-->
<setting name="useGeneratedKeys" value="true"/>
<!-- 使用标签替换列别名 默认:true-->
<setting name="useColumnLabel" value="true"/>
<!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime}-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
4、修改aplication.properties 文件,添加以下配置
(注意:mapp文件和entity文件路径必须与实际一致!)
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/devops
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#配置.xml文件路径
mybatis.mapper-locations=classpath*:/mapping/**.xml
#配置模型路径
mybatis.type-aliases-package=com.example.demo.domain
5、resouces下面添加mapping文件夹,并添加 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.example.demo.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.example.demo.domain.User" >
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="VARCHAR" />
<result column="sex" property="sex" jdbcType="VARCHAR" />
<result column="mobile" property="mobile" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, name, age, sex, mobile
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from user
where id = #{id,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.example.demo.domain.User" >
insert into user (id, name, age,
sex, mobile)
values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.example.demo.domain.User" >
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="name != null" >
name,
</if>
<if test="age != null" >
age,
</if>
<if test="sex != null" >
sex,
</if>
<if test="mobile != null" >
mobile,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=VARCHAR},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="age != null" >
#{age,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
#{sex,jdbcType=VARCHAR},
</if>
<if test="mobile != null" >
#{mobile,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.example.demo.domain.User" >
update user
<set >
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="age != null" >
age = #{age,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
sex = #{sex,jdbcType=VARCHAR},
</if>
<if test="mobile != null" >
mobile = #{mobile,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.example.demo.domain.User" >
update user
set name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
mobile = #{mobile,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
</mapper>
6、在com.example.demo下面分别添加以下文件夹
(1)domain文件夹,存放数据库实体,添加User.java
package com.example.demo.domain;
public class User {
private String id;
private String name;
private String age;
private String sex;
private String mobile;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id == null ? null : id.trim();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age == null ? null : age.trim();
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile == null ? null : mobile.trim();
}
}
(2)添加dao文件夹,添加UserMapper.java
package com.example.demo.domain;
public class User {
private String id;
private String name;
private String age;
private String sex;
private String mobile;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id == null ? null : id.trim();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age == null ? null : age.trim();
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile == null ? null : mobile.trim();
}
}
(3)添加service文件夹,添加接口类 IUserService,添加文件夹impl ,添加UserServiceImpl.java
#IUserService.java
package com.example.demo.service;
import com.example.demo.domain.User;
public interface IUserService {
public User getUser(String id);
public User selectByPrimaryKey(String id);
}
#UserServiceImpl.java
package com.example.demo.service.impl;
import com.example.demo.dao.UserMapper;
import com.example.demo.domain.User;
import com.example.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
public User getUser(String id){
return userMapper.selectByPrimaryKey(id);
}
@Override
public User selectByPrimaryKey(String id) {
return userMapper.selectByPrimaryKey(id);
}
}
7、修改controller下面TestController类,添加如下配置
package com.example.demo.controller;
import com.example.demo.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.example.demo.domain.User;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TestController {
private static Logger logger = LoggerFactory.getLogger(TestController.class);
@RequestMapping("/index")
public String home() {
logger.info(">>> index");
return "index";
}
@RequestMapping("/tables")
public String table(HttpServletRequest request){
logger.info(">>> tables");
request.setAttribute("name", "zhangjf");
return "tables";
}
@Autowired
private IUserService userService;
@RequestMapping("/user")
public String getItem(HttpServletRequest request) {
String id = request.getParameter("id");
logger.info("进入getItem方法,id:"+id);
User user = userService.selectByPrimaryKey(id);
request.setAttribute("user", user);
return user.getName();
}
}