0、前言
mybatis属于半自动的ORM,相比hibernate这种全自动的ORM,兼顾了性能与易用;目前企业项目中,基本都是mybatis的天下;今天就来整合mybatis与MySQL;
1、整合
1.-1、添加依赖:
<!-- 集成mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency>
1-2、创建数据表:
-- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(32) NOT NULL AUTO_INCREMENT COMMENT '用户ID', `userName` varchar(32) NOT NULL COMMENT '用户名称', `passWord` varchar(50) NOT NULL COMMENT '用户密码', `realName` varchar(32) DEFAULT NULL COMMENT '中文名字', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1', 'anson', '123', '张三'); INSERT INTO `user` VALUES ('2', 'Alex', '123', '李四'); INSERT INTO `user` VALUES ('3', 'kks', '123', '王五');
1-3、增加实体类User.java
package com.anson.model; public class User { private Integer id; private String username; private String password; private String realname;
public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username == null ? null : username.trim(); } public String getPassword() { return password; } public void setPassword(String password) { this.password = password == null ? null : password.trim(); } public String getRealname() { return realname; } public void setRealname(String realname) { this.realname = realname == null ? null : realname.trim(); } }
1-4、增加Mapper接口UserMapper.java;注意添加@Repository注解
package com.anson.dao; import com.anson.model.User; import org.springframework.stereotype.Repository; @Repository //添加Repository注解 public interface UserMapper { int deleteByPrimaryKey(Integer id); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); }
1-5、添加Mapper对应的XML文件UserMapper.xml,注意<mapper namespace="com.anson.dao.UserMapper">对应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.anson.dao.UserMapper"> <resultMap > <id column="id" jdbcType="INTEGER" property="id" /> <result column="userName" jdbcType="VARCHAR" property="username" /> <result column="passWord" jdbcType="VARCHAR" property="password" /> <result column="realName" jdbcType="VARCHAR" property="realname" /> </resultMap> <sql > id, userName, passWord, realName </sql> <select > select <include ref> from user where id = #{id,jdbcType=INTEGER} </select> <delete > delete from user where id = #{id,jdbcType=INTEGER} </delete> <insert > insert into user (id, userName, passWord, realName) values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{realname,jdbcType=VARCHAR}) </insert> <insert > insert into user <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="username != null"> userName, </if> <if test="password != null"> passWord, </if> <if test="realname != null"> realName, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=INTEGER}, </if> <if test="username != null"> #{username,jdbcType=VARCHAR}, </if> <if test="password != null"> #{password,jdbcType=VARCHAR}, </if> <if test="realname != null"> #{realname,jdbcType=VARCHAR}, </if> </trim> </insert> <update > update user <set> <if test="username != null"> userName = #{username,jdbcType=VARCHAR}, </if> <if test="password != null"> passWord = #{password,jdbcType=VARCHAR}, </if> <if test="realname != null"> realName = #{realname,jdbcType=VARCHAR}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update > update user set userName = #{username,jdbcType=VARCHAR}, passWord = #{password,jdbcType=VARCHAR}, realName = #{realname,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> </mapper>
View Code
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:小白的springboot之路(三)、集成mybatis与MySQL - Python技术站