1、导入Mybatis及依赖 - mybatis-spring-boot-starter
<!--WEB启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--JDBC--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--MySQL驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--Mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
2、application.yml - 配置dataSource与整合Mybatis
# spring配置 spring: datasource: # data数据源 username: root password: 123456 url: jdbc:mysql://localhost:3306/games_db?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=GMT%2B8 driver-class-name: com.mysql.cj.jdbc.Driver # 整合Mybatis mybatis: type-aliases-package: com.cyan.pojo # 配置别名 mapper-locations: classpath:mappers/*.xml # mapper.xml配置文件
3、SpringBootTest测试数据库是否连接成功!
@SpringBootTest class Springboot05MybatisApplicationTests { @Autowired private DataSource dataSource; @Test void contextLoads() throws SQLException { System.out.println(dataSource.getClass()); System.out.println(dataSource.getConnection()); } }
4、新建pojo包,使用Lombok创建实体类
package com.cyan.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.math.BigDecimal; import java.util.Date; @Data @NoArgsConstructor @AllArgsConstructor public class User { private Integer id; private String username; private String userpassword; private BigDecimal balance; private Date grgisterdate; }
package com.cyan.pojo; import lombok.*; @Data @NoArgsConstructor @AllArgsConstructor public class Role { private Integer idrole; private String name; private Integer level; private String roletype; private Integer userid; }
5、新建mapper包,创建Mapper接口
package com.cyan.mapper; import com.cyan.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper // 表示这个类是Mybatis的Mapper类; @Repository // Dao层; public interface UserMapper { List<User> getUsers();// 查询全部 User getUserById(Integer id);// 根据ID查询 Integer saveUser(User user);// 添加User Integer updateUser(User user);// 修改User Integer deleteUser(Integer id);// 根据ID删除 }
package com.cyan.mapper; import com.cyan.pojo.Role; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper // 表示这个类是Mybatis的Mapper类; @Repository // Dao层; public interface RoleMapper { List<Role> getRoles();// 查询全部 Role getRoleById(Integer idrole);// 根据ID查询 Integer saveRole(Role role);// 添加Role Integer updateRole(Role role);// 修改ROle Integer deleteRole(Integer idrole);// 根据ID删除 }
6、在resources目录下新建mappers文件夹,创建对应的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.cyan.mapper.UserMapper"> <select id="getUsers" resultType="User"> SELECT * FROM user </select> <select id="getUserById" parameterType="integer" resultType="User"> SELECT * FROM user WHERE id = #{id} </select> <insert id="saveUser" parameterType="User"> INSERT INTO user VALUES(#{id},#{username},#{userpassword},#{balance},#{grgisterdate}) </insert> <update id="updateUser" parameterType="User"> UPDATE user SET username=#{username},userpassword=#{userpassword},balance=#{balance},grgisterdate=#{grgisterdate} WHERE id = #{id} </update> <delete id="deleteUser" parameterType="Integer"> DELETE FROM user WHERE id = #{id} </delete> </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.cyan.mapper.RoleMapper"> <select id="getRoles" resultType="Role"> SELECT * FROM role </select> <select id="getRoleById" parameterType="Integer" resultType="Role"> SELECT * FROM role WHERE idrole = #{idrole} </select> <insert id="saveRole" parameterType="Role"> INSERT INTO role VALUES(#{idrole},#{name},#{level},#{roletype},#{userid}) </insert> <update id="updateRole" parameterType="Role"> UPDATE role SET name=#{name},level=#{level},roletype=#{roletype},userid=#{userid} WHERE idrole = #{idrole} </update> <delete id="deleteRole" parameterType="Integer"> DELETE FROM role WHERE idrole = #{idrole} </delete> </mapper>
7、直接创建controller层测试,这里就不返回页面了,只访问路径测试。
package com.cyan.controller; import com.cyan.mapper.UserMapper; import com.cyan.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.math.BigDecimal; import java.util.Date; import java.util.List; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserMapper userMapper; //访问list返回list集合对象 @RequestMapping("/list") public List<User> getUsers(){ List<User> users = userMapper.getUsers(); return users; } //访问get返回指定ID的user对象 @RequestMapping("/get") public User get(){ User user = userMapper.getUserById(119); return user; } //访问save添加user到数据库 @RequestMapping("/save") public String save(){ userMapper.saveUser(new User(119,"username","userpassword", BigDecimal.valueOf(10000),new Date())); return "save-OK"; } //访问update修改数据库中的user @RequestMapping("/update") public String update(){ userMapper.updateUser(new User(119,"青山不语","qingshanbuyu", BigDecimal.valueOf(9999),new Date())); return "update-OK"; } //访问delete删除指定ID对象 @RequestMapping("/delete") public String delete(){ userMapper.deleteUser(119); return "delete-OK"; } }
package com.cyan.controller; import com.cyan.mapper.RoleMapper; import com.cyan.pojo.Role; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController() @RequestMapping("/role") public class RoleController { @Autowired private RoleMapper roleMapper; //访问list返回list集合对象 @RequestMapping("/list") public List<Role> getRoles(){ List<Role> roles = roleMapper.getRoles(); return roles; } //访问get返回指定ID的role对象 @RequestMapping("/get") public Role get(){ Role role = roleMapper.getRoleById(12); return role; } //访问save添加role到数据库 @RequestMapping("/save") public String save(){ roleMapper.saveRole(new Role(12,"镜",15,"刺客",5)); return "save-OK"; } //访问update修改数据库中的role @RequestMapping("/update") public String update(){ roleMapper.updateRole(new Role(12,"镜",0,"刺客",5)); return "update-OK"; } //访问delete删除指定ID对象 @RequestMapping("/delete") public String delete(){ roleMapper.deleteRole(12); return "delete-OK"; } }
8、启动项目,简单访问几个路径进行测试
- /role/list
- /user/save
- /user/get
- /user/update
- /user/delete
测试结果完成,搞定收工!!!
原文:https://www.cnblogs.com/Dm920/p/12626355.html