一、项目建立
建立一个简单的maven 项目,加入以下pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
二、配置application.properties
# mysql
spring.datasource.url=jdbc:mysql://localhost/springboot?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath:mybatis/*.xml
#增加控制台打印sql语句,一般用于本地开发测试
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
***mybatis.mapper-locations=classpath:mybatis/*.xml 表示扫描resources/mybatis目录下所有xml文件作为mapper
三、配置mapper
namespace="com.wqc.mapper.UserMapper" 必须指向mappper接口的全类名
<?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.wqc.mapper.UserMapper">
<resultMap type="com.wqc.model.User" id="userMap">
<id column="id" property="id" />
<result column="user_name" property="userName" />
<result column="pass_word" property="passWord" />
<result column="create_time" property="createTime" />
</resultMap>
<select id="getUsers" resultType="com.wqc.model.User"
resultMap="userMap">
select * from users
</select>
<select id="update" parameterType="com.wqc.model.User">
update users set
user_name=#{userName},pass_word=#{passWord},create_time=#{createTime}
where
id=#{id}
</select>
<select id="del" parameterType="java.lang.Integer">
delete from users where id=#{id}
</select>
<select id="save" parameterType="com.wqc.model.User">
insert into users(user_name,pass_word,create_time) values(#{userName},#{passWord},#{createTime})
</select>
</mapper>
四、编写mapper接口
package com.wqc.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;
import com.wqc.model.User;
@Mapper
public interface UserMapper {
List<User> getUsers();
void update(User user);
void del(@Param("id")int id);
void save(User user);
}
五、编写controller
package com.wqc.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wqc.model.User;
import com.wqc.mapper.UserMapper;
@Controller
@RequestMapping(value = "/")
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping(value="/users",method=RequestMethod.GET)
@ResponseBody
public List<User> getUsers() {
List<User> userList = userMapper.getUsers();
return userList;
}
@ResponseBody
@RequestMapping(value="/update",method=RequestMethod.POST)
public List<User> update() {
User user=new User();
user.setId(1);
user.setUserName("updateName");
user.setPassWord("123");
user.setCreateTime(new Date());
userMapper.update(user);
return userMapper.getUsers();
}
@ResponseBody
@RequestMapping(value="/del",method=RequestMethod.POST)
public List<User> del(){
userMapper.del(1);
return userMapper.getUsers();
}
@ResponseBody
@RequestMapping(value="add",method=RequestMethod.POST)
public List<User> add(){
User user=new User();
user.setUserName("test");
user.setPassWord("111");
user.setCreateTime(new Date());
userMapper.save(user);
return userMapper.getUsers();
}
}
六、启动类
@SpringBootApplication
@MapperScan("com.wqc.mapper") //如果每个mapper文件自带了@Mapper,则这里可以不写
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
七、运行中打印sql问题
只要在log4j的日志级别中改为debug就能打印出sql