(二十一)SpringBoot之集成mybatis:使用mybatis xml
一、引入maven依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> </dependencies>
二、配置application.properties
# mysql spring.datasource.url=jdbc:mysql://localhost/db_boot?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password= spring.datasource.driver-class-name=com.mysql.jdbc.Driver logging.level.com.shyroke.mapper=debug mybatis.mapper-locations=classpath:mybatis/*.xml
- mybatis.mapper-locations=classpath:mybatis/*.xml 表示扫描mybatis目录下所有xml文件作为mapper
三、配置mapper
-
namespace="com.shyroke.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.shyroke.mapper.UserMapper"> <resultMap type="com.shyroke.bean.UserBean" 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.shyroke.bean.UserBean" resultMap="userMap"> select * from users </select> <select id="update" parameterType="com.shyroke.bean.UserBean"> 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.shyroke.bean.UserBean"> insert into users(user_name,pass_word,create_time) values(#{userName},#{passWord},#{createTime}) </select> </mapper>
四、编写mapper接口
package com.shyroke.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.shyroke.bean.UserBean; @Mapper public interface UserMapper { /** * 获取所有用户 * * @return */ List<UserBean> getUsers(); /** * 修改用户信息 * * @param user */ void update(UserBean user); /** * 删除用户 * * @param id用户id */ void del(@Param("id")int id); /** * 新增一条用户信息 * * @param user */ void save(UserBean user); }
五、编写控制器
package com.shyroke.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.shyroke.bean.UserBean; import com.shyroke.mapper.UserMapper; @Controller @RequestMapping(value = "/") public class UserController { @Autowired private UserMapper userMapper; @RequestMapping(value="/users") @ResponseBody public List<UserBean> getUsers() { List<UserBean> userList = userMapper.getUsers(); return userList; } @ResponseBody @RequestMapping(value="/update") public List<UserBean> update() { UserBean user=new UserBean(); user.setId(1); user.setUserName("updateName"); user.setPassWord("123"); user.setCreateTime(new Date()); userMapper.update(user); return userMapper.getUsers(); } @ResponseBody @RequestMapping(value="/del") public List<UserBean> del(){ userMapper.del(1); return userMapper.getUsers(); } @ResponseBody @RequestMapping(value="add") public List<UserBean> add(){ UserBean user=new UserBean(); user.setUserName("test"); user.setPassWord("111"); user.setCreateTime(new Date()); userMapper.save(user); return userMapper.getUsers(); } }
六、测试结果与上一章节一样