Introduction of depend on
-
Introduce mybatis-spring-boot-starter, mysql-connector-java in POM file.
-
Mybatis – spring – the boot – starter, mysql connector – Java
The < mybatis. Spring. Version > 1.3.2 < / mybatis. Spring. Version >Copy the code
<! -- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.version}</version> </dependency> <! -- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>Copy the code
- Complete POM file
<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.4.5 < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.gold</groupId> <artifactId>gj-tmall</artifactId> <version>0.0.1-SNAPSHOT</version> <name> GJ-tmall </name> <description> Build SpringBoot project </description> <properties> < Java version > 1.8 < / Java version > < mybatis. Spring. Version > 1.3.2 < / mybatis. Spring. Version > < / properties > < dependencies > <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! -- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.version}</version> </dependency> <! -- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code
Add the configuration
Configure MyBatis
Create the com.gold.tmall.config package, and create a MyBatis configuration class mybatisconfig. Java, configure the related scanning path, including DAO, Model, XML mapping file scanning.
MyBatisConfig.java
package com.gold.tmall.config; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.annotation.Resource; import javax.sql.DataSource; /** * MapperScan scan DAO * @author zhang. Fuiqng * @date 2021-04-18 21:13:00 */ @configuration @MapperScan("com.gold.tmall.**.dao") public class MyBatisConfig { @Resource private DataSource dataSource; @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); / / scanning Model sessionFactory. SetTypeAliasesPackage (" com. Gold. Tmall. * *. The Model "); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); / / scanning mapping file sessionFactory. SetMapperLocations (resolver. GetResources (" classpath * : / sqlmap / *. * * XML ")); return sessionFactory.getObject(); }}Copy the code
- (You may need to change the package name and XML file path to match the actual project)
Configuring a Data Source
In application.yml, add MySQL data source connection information and configure port 8088. Change the name of the file application.properties to: application.yml.
Server: port: 8088 spring: a datasource: driverClassName: com. Mysql. Cj). The JDBC Driver url: JDBC: mysql: / / 127.0.0.1:3306 / tmall? useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8 username: root password: 123456Copy the code
Build database build table add data
DROP TABLE IF EXISTS `sys_user`; CREATE TABLE 'sys_user' (' id 'bigint(20) NOT NULL AUTO_INCREMENT COMMENT' unsigned ', 'name' varchar(50) NOT NULL COMMENT 'iD ',' nick_name 'varchar(150) DEFAULT NULL COMMENT' iD ', 'Avatar' varchar(150) DEFAULT NULL COMMENT 'avatar ',' password 'varchar(100) DEFAULT NULL COMMENT' password ', 'salt' varchar(100) DEFAULT NULL COMMENT 'iD ',' email 'varchar(100) DEFAULT NULL COMMENT' id ', 'mobile' varchar(100) DEFAULT NULL COMMENT 'mobile phone number ',' status' tinyInt (4) DEFAULT NULL COMMENT 'Status 0: Disabled 1: Dept_id bigint(20) DEFAULT NULL COMMENT 'ID',' create_by 'varchar(50) DEFAULT NULL COMMENT' founder ', 'create_time' datetime DEFAULT NULL COMMENT 'create time ',' last_update_by 'varchar(50) DEFAULT NULL COMMENT' create time ', 'last_update_by' varchar(50) DEFAULT NULL COMMENT 'create time ', 'last_update_time' datetime DEFAULT NULL COMMENT 'update_time ',' del_flag 'tinyint(4) DEFAULT '0' COMMENT' Whether to delete -1: delete 0: PRIMARY KEY (' id '); ENGINE=InnoDB AUTO_INCREMENT= 35 DEFAULT CHARSET=utf8 COMMENT=' unsigned '; INSERT INTO ` sys_user ` VALUES (' 1 ', 'admin', 'super tube, null,' bd1718f058d8a02468134432b8656a86 ', 'YzcmCZNvbXocrsz9dm8e', '[email protected]', '13612345678', '1', '4', 'admin', '2018-08-14 11:11:11', 'admin', '2018-08-14 11:11:11', '0'); INSERT INTO ` sys_user ` VALUES (' 22 ', 'liubei', 'liu bei, null,' fd80ebd493a655608dc893a9f897d845 ', 'YzcmCZNvbXocrsz9dm8e', '[email protected]', '13889700023', '1', '7', 'admin', '2018-09-23 19:43:00', 'admin', '2019-01-10 11:41:13', '0'); INSERT INTO ` sys_user ` VALUES (' 23 ', 'zhaoyun', 'zhaoyun, null,' fd80ebd493a655608dc893a9f897d845 ', 'YzcmCZNvbXocrsz9dm8e', '[email protected]', '13889700023', '1', '7', 'admin', '2018-09-23 19:43:44', 'admin', '2018-09-23 19:43:52', '0'); INSERT INTO ` sys_user ` VALUES (' 24 ', 'zhugeliang, zhuge liang, null,' fd80ebd493a655608dc893a9f897d845 ', 'YzcmCZNvbXocrsz9dm8e', '[email protected]', '13889700023', '7', '11', 'admin', '2018-09-23 19:44:23', 'admin', 'the 2018-09-23 19:44:29', '0'); INSERT INTO ` sys_user ` VALUES (' 25 ', 'caocao, cao cao, null,' fd80ebd493a655608dc893a9f897d845 ', 'YzcmCZNvbXocrsz9dm8e', '[email protected]', '13889700023', '1', '8', 'admin', '2018-09-23 19:45:32', 'admin', '2019-01-10 17:59:14', '0'); INSERT INTO ` sys_user ` VALUES (' 26 ', 'dianwei', 'dian wei, null,' fd80ebd493a655608dc893a9f897d845 ', 'YzcmCZNvbXocrsz9dm8e', '[email protected]', '13889700023', '1', '10', 'admin', '2018-09-23 19:45:48', 'admin', '2018-09-23 19:45:57', '0'); INSERT INTO ` sys_user ` VALUES (' 27 ', 'xiahoudun', ''s 'null' fd80ebd493a655608dc893a9f897d845 ', 'YzcmCZNvbXocrsz9dm8e', '[email protected]', '13889700023', '1', '8', 'admin', '2018-09-23 19:46:09', 'admin', 'the 2018-09-23 19:46:17', '0'); INSERT INTO ` sys_user ` VALUES (' 28 ', 'xunyu', 'xun yu, null,' fd80ebd493a655608dc893a9f897d845 ', 'YzcmCZNvbXocrsz9dm8e', '[email protected]', '13889700023', '1', '10', 'admin', '2018-09-23 19:46:38', 'admin', '2018-11-04 15:33:17', '0'); INSERT INTO ` sys_user ` VALUES (' 29 ', 'sunquan', 'sun quan, null,' fd80ebd493a655608dc893a9f897d845 ', 'YzcmCZNvbXocrsz9dm8e', '[email protected]', '13889700023', '1', '10', 'admin', '2018-09-23 19:46:54', 'admin', '2018-09-23 19:47:03', '0'); INSERT INTO ` sys_user ` VALUES (' 30 ', 'zhouyu, "zhou yu", null,' fd80ebd493a655608dc893a9f897d845 ', 'YzcmCZNvbXocrsz9dm8e', '[email protected]', '13889700023', '1', '11', 'admin', '2018-09-23 19:47:28', 'admin', '2018-09-23 19:48:04', '0'); INSERT INTO ` sys_user ` VALUES (' and ', 'luxun', 'tracing the cause of null,' fd80ebd493a655608dc893a9f897d845 ', 'YzcmCZNvbXocrsz9dm8e', '[email protected]', '13889700023', '1', '11', 'admin', '2018-09-23 19:47:44', 'admin', '2018-09-23 19:47:58', '0'); INSERT INTO ` sys_user ` VALUES (' 32 ', 'huanggai', 'huang gai, null,' fd80ebd493a655608dc893a9f897d845 ', 'YzcmCZNvbXocrsz9dm8e', '[email protected]', '13889700023', '1', '11', 'admin', '2018-09-23 19:48:38', 'admin', 'the 2018-09-23 19:49:02', '0');Copy the code
Write the interface
Build package
- com.gold.tmall.model
- com.gold.tmall.dao
- com.gold.tmall.sqlmap
- com.gold.tmall.service
- com.gold.tmall.service.impl
- com.gold.tmall.controller
To build such
- com.gold.tmall.model.SysUser
- com.gold.tmall.dao.SysUserMapper
- com.gold.tmall.sqlmap.SysUserMapper.xml
- com.gold.tmall.service.SysUserService
- com.gold.tmall.service.impl.SysUserServiceImpl
- com.gold.tmall.controller.SysUserController
write
- com.gold.tmall.model.SysUser
package com.gold.tmall.model;
import java.util.Date;
import java.util.Objects;
/**
* @author zhang.fuqing
* @date 2021-04-16 21:03:14
*/
public class SysUser {
/**
* 编号
*/
private Integer id;
/**
* 用户名
*/
private String name;
/**
* 昵称
*/
private String nickName;
/**
* 头像
*/
private String avatar;
/**
* 密码
*/
private String password;
/**
* 加密盐
*/
private String salt;
/**
* 邮箱
*/
private String email;
/**
* 手机号
*/
private String mobile;
/**
* 状态 0:禁用 1:正常
*/
private Integer status;
/**
* 机构ID
*/
private Integer deptId;
/**
* 创建人
*/
private String createBy;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新人
*/
private String lastUpdateBy;
/**
* 更新时间
*/
private Date lastUpdateTime;
/**
* 是否删除 -1:已删除 0:正常
*/
private Integer delFlag;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getLastUpdateBy() {
return lastUpdateBy;
}
public void setLastUpdateBy(String lastUpdateBy) {
this.lastUpdateBy = lastUpdateBy;
}
public Date getLastUpdateTime() {
return lastUpdateTime;
}
public void setLastUpdateTime(Date lastUpdateTime) {
this.lastUpdateTime = lastUpdateTime;
}
public Integer getDelFlag() {
return delFlag;
}
public void setDelFlag(Integer delFlag) {
this.delFlag = delFlag;
}
@Override
public String toString() {
return "SysUser{" +
"id=" + id +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
", avatar='" + avatar + '\'' +
", password='" + password + '\'' +
", salt='" + salt + '\'' +
", email='" + email + '\'' +
", mobile='" + mobile + '\'' +
", status=" + status +
", deptId=" + deptId +
", createBy='" + createBy + '\'' +
", createTime=" + createTime +
", lastUpdateBy='" + lastUpdateBy + '\'' +
", lastUpdateTime=" + lastUpdateTime +
", delFlag=" + delFlag +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SysUser sysUser = (SysUser) o;
if (!Objects.equals(id, sysUser.id)) {
return false;
}
if (!Objects.equals(name, sysUser.name)) {
return false;
}
if (!Objects.equals(nickName, sysUser.nickName)) {
return false;
}
if (!Objects.equals(avatar, sysUser.avatar)) {
return false;
}
if (!Objects.equals(password, sysUser.password)) {
return false;
}
if (!Objects.equals(salt, sysUser.salt)) {
return false;
}
if (!Objects.equals(email, sysUser.email)) {
return false;
}
if (!Objects.equals(mobile, sysUser.mobile)) {
return false;
}
if (!Objects.equals(status, sysUser.status)) {
return false;
}
if (!Objects.equals(deptId, sysUser.deptId)) {
return false;
}
if (!Objects.equals(createBy, sysUser.createBy)) {
return false;
}
if (!Objects.equals(createTime, sysUser.createTime)) {
return false;
}
if (!Objects.equals(lastUpdateBy, sysUser.lastUpdateBy)) {
return false;
}
if (!Objects.equals(lastUpdateTime, sysUser.lastUpdateTime)) {
return false;
}
return Objects.equals(delFlag, sysUser.delFlag);
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (nickName != null ? nickName.hashCode() : 0);
result = 31 * result + (avatar != null ? avatar.hashCode() : 0);
result = 31 * result + (password != null ? password.hashCode() : 0);
result = 31 * result + (salt != null ? salt.hashCode() : 0);
result = 31 * result + (email != null ? email.hashCode() : 0);
result = 31 * result + (mobile != null ? mobile.hashCode() : 0);
result = 31 * result + (status != null ? status.hashCode() : 0);
result = 31 * result + (deptId != null ? deptId.hashCode() : 0);
result = 31 * result + (createBy != null ? createBy.hashCode() : 0);
result = 31 * result + (createTime != null ? createTime.hashCode() : 0);
result = 31 * result + (lastUpdateBy != null ? lastUpdateBy.hashCode() : 0);
result = 31 * result + (lastUpdateTime != null ? lastUpdateTime.hashCode() : 0);
result = 31 * result + (delFlag != null ? delFlag.hashCode() : 0);
return result;
}
}
Copy the code
- com.gold.tmall.dao.SysUserMapper.java
package com.gold.tmall.dao; import java.util.List; import com.gold.tmall.model.SysUser; /** * @author zhang.fuqing * @date 2021-04-16 21:03:14 */ public interface SysUserMapper {/** ** Delete user information * @param Id User primary key * @return Number of deletions */ int deleteByPrimaryKey(Long ID); /** * insert user info * @param record user info * @return insert number */ int insert(SysUser record); /** * Select a non-empty field to insert user information * @param record user information * @return insert number */ int insertSelective(SysUser record); /** * Query user information based on the user primary key * @param ID User primary key * @return user information */ SysUser selectByPrimaryKey(Long ID); / * * * according to the user home key update user information is not empty * @ param record is updated the user is not empty information * @ return update article number * / int updateByPrimaryKeySelective (SysUser record); /** * update user information according to user primary key * @param record update information * @return update number */ int updateByPrimaryKey(SysUser record); @sysuser */ List<SysUser> findAll(); }Copy the code
- com.gold.tmall.sqlmap.SysUserMapper.xml
(Tip: replace com.gold. Tmall according to the actual project package name)
<?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.gold.tmall.dao.SysUserMapper">
<resultMap id="BaseResultMap" type="com.gold.tmall.model.SysUser">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="nick_name" jdbcType="VARCHAR" property="nickName" />
<result column="avatar" jdbcType="VARCHAR" property="avatar" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="salt" jdbcType="VARCHAR" property="salt" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="mobile" jdbcType="VARCHAR" property="mobile" />
<result column="status" jdbcType="TINYINT" property="status" />
<result column="dept_id" jdbcType="BIGINT" property="deptId" />
<result column="create_by" jdbcType="VARCHAR" property="createBy" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="last_update_by" jdbcType="VARCHAR" property="lastUpdateBy" />
<result column="last_update_time" jdbcType="TIMESTAMP" property="lastUpdateTime" />
<result column="del_flag" jdbcType="TINYINT" property="delFlag" />
</resultMap>
<sql id="Base_Column_List">
id, name, nick_name, avatar, password, salt, email, mobile, status, dept_id, create_by,
create_time, last_update_by, last_update_time, del_flag
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from sys_user
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from sys_user
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="com.gold.tmall.model.SysUser">
insert into sys_user (id, name, nick_name,
avatar, password, salt,
email, mobile, status,
dept_id, create_by, create_time,
last_update_by, last_update_time, del_flag
)
values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{nickName,jdbcType=VARCHAR},
#{avatar,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{salt,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR}, #{status,jdbcType=TINYINT},
#{deptId,jdbcType=BIGINT}, #{createBy,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP},
#{lastUpdateBy,jdbcType=VARCHAR}, #{lastUpdateTime,jdbcType=TIMESTAMP}, #{delFlag,jdbcType=TINYINT}
)
</insert>
<insert id="insertSelective" parameterType="com.gold.tmall.model.SysUser">
insert into sys_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="nickName != null">
nick_name,
</if>
<if test="avatar != null">
avatar,
</if>
<if test="password != null">
password,
</if>
<if test="salt != null">
salt,
</if>
<if test="email != null">
email,
</if>
<if test="mobile != null">
mobile,
</if>
<if test="status != null">
status,
</if>
<if test="deptId != null">
dept_id,
</if>
<if test="createBy != null">
create_by,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="lastUpdateBy != null">
last_update_by,
</if>
<if test="lastUpdateTime != null">
last_update_time,
</if>
<if test="delFlag != null">
del_flag,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="nickName != null">
#{nickName,jdbcType=VARCHAR},
</if>
<if test="avatar != null">
#{avatar,jdbcType=VARCHAR},
</if>
<if test="password != null">
#{password,jdbcType=VARCHAR},
</if>
<if test="salt != null">
#{salt,jdbcType=VARCHAR},
</if>
<if test="email != null">
#{email,jdbcType=VARCHAR},
</if>
<if test="mobile != null">
#{mobile,jdbcType=VARCHAR},
</if>
<if test="status != null">
#{status,jdbcType=TINYINT},
</if>
<if test="deptId != null">
#{deptId,jdbcType=BIGINT},
</if>
<if test="createBy != null">
#{createBy,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="lastUpdateBy != null">
#{lastUpdateBy,jdbcType=VARCHAR},
</if>
<if test="lastUpdateTime != null">
#{lastUpdateTime,jdbcType=TIMESTAMP},
</if>
<if test="delFlag != null">
#{delFlag,jdbcType=TINYINT},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.gold.tmall.model.SysUser">
update sys_user
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="nickName != null">
nick_name = #{nickName,jdbcType=VARCHAR},
</if>
<if test="avatar != null">
avatar = #{avatar,jdbcType=VARCHAR},
</if>
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
</if>
<if test="salt != null">
salt = #{salt,jdbcType=VARCHAR},
</if>
<if test="email != null">
email = #{email,jdbcType=VARCHAR},
</if>
<if test="mobile != null">
mobile = #{mobile,jdbcType=VARCHAR},
</if>
<if test="status != null">
status = #{status,jdbcType=TINYINT},
</if>
<if test="deptId != null">
dept_id = #{deptId,jdbcType=BIGINT},
</if>
<if test="createBy != null">
create_by = #{createBy,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="lastUpdateBy != null">
last_update_by = #{lastUpdateBy,jdbcType=VARCHAR},
</if>
<if test="lastUpdateTime != null">
last_update_time = #{lastUpdateTime,jdbcType=TIMESTAMP},
</if>
<if test="delFlag != null">
del_flag = #{delFlag,jdbcType=TINYINT},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="com.gold.tmall.model.SysUser">
update sys_user
set name = #{name,jdbcType=VARCHAR},
nick_name = #{nickName,jdbcType=VARCHAR},
avatar = #{avatar,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
salt = #{salt,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
mobile = #{mobile,jdbcType=VARCHAR},
status = #{status,jdbcType=TINYINT},
dept_id = #{deptId,jdbcType=BIGINT},
create_by = #{createBy,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=TIMESTAMP},
last_update_by = #{lastUpdateBy,jdbcType=VARCHAR},
last_update_time = #{lastUpdateTime,jdbcType=TIMESTAMP},
del_flag = #{delFlag,jdbcType=TINYINT}
where id = #{id,jdbcType=BIGINT}
</update>
<select id="findAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from sys_user
</select>
</mapper>
Copy the code
- com.gold.tmall.service.SysUserService
package com.gold.tmall.service; import java.util.List; import com.gold.tmall.model.SysUser; /** * @author zhang. Fuqing * @date 2021-04-16 21:03:14 */ public interface SysUserService {** ** find all users * @return */ List<SysUser> findAll(); /** * Query user information based on the user primary key * @param ID User primary key * @return user information */ SysUser selectByPrimaryKey(Long ID); /** * insert user info * @param record user info * @return insert number */ int insert(SysUser record); /** * Delete user information based on user primary key * @param ID User primary key * @return Delete number */ int deleteByPrimaryKey(Long ID); }Copy the code
- com.gold.tmall.service.impl.SysUserServiceImpl
package com.gold.tmall.service.impl; import com.gold.tmall.dao.SysUserMapper; import com.gold.tmall.model.SysUser; import com.gold.tmall.service.SysUserService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; /** * @author zhang.fuqing * @date 2021-04-16 21:04:14 */ @Service public class SysUserServiceImpl implements SysUserService { @Resource private SysUserMapper sysUserMapper; @Override public SysUser selectByPrimaryKey(Long id) { return sysUserMapper.selectByPrimaryKey(id); } @Override public int insert(SysUser record) { return sysUserMapper.insert(record); } @Override public int deleteByPrimaryKey(Long id) { return sysUserMapper.deleteByPrimaryKey(id); } @Override public List<SysUser> findAll() { return sysUserMapper.findAll(); }}Copy the code
- com.gold.tmall.controller.SysUserController
package com.gold.tmall.controller; import com.gold.tmall.model.SysUser; import com.gold.tmall.service.SysUserService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author zhang.fuqing * @date 2021-04-16 20:59:14 */ @RestController public class SysUserController { @Resource private SysUserService sysUserService; @GetMapping(value="/api/user/findAll") public Object findAll() { return sysUserService.findAll(); } @GetMapping(value="/api/user/{id}") public SysUser getUser(@PathVariable Long id) { return sysUserService.selectByPrimaryKey(id); }}Copy the code
- com.gold.tmall.TmallApplication
Add @SpringBootApplication(scanBasePackages={“com.gold. Tmall “}) to tmallApplication.java
package com.gold.tmall; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author zhang.fuqing * @date 2021-04-18 22:04:16 */ @SpringBootApplication(scanBasePackages={"com.gold.tmall"}) public class TmallApplication { public static void main(String[] args) { SpringApplication.run(TmallApplication.class, args); }}Copy the code
Packaging resources
When compiling the package, the XML mapping file is not included in the package by default, causing an “Invalid bound Statement (not found)” error. Modify pom.xml to add the following to the build tag, and the mapping file will be copied when the code is packaged.
<! <resources> <resource> <directory> SRC /main/ Java </directory> <includes> <include>**/sqlmap/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> <filtering>true</filtering> </resource> </resources>Copy the code
The complete POM.xml file
<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.4.5 < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.gold</groupId> <artifactId>gj-tmall</artifactId> <version>0.0.1-SNAPSHOT</version> <name> GJ-tmall </name> <description> Build SpringBoot project </description> <properties> < Java version > 1.8 < / Java version > < mybatis. Spring. Version > 1.3.2 < / mybatis. Spring. Version > < / properties > < dependencies > <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! -- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.version}</version> </dependency> <! -- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <! <resources> <resource> <directory> SRC /main/ Java </directory> <includes> <include>**/sqlmap/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> <filtering>true</filtering> </resource> </resources> </build> </project>Copy the code
A test run
Start the application and access:
http://localhost:8088/api/user/findAll
http://localhost:8088/api/user/1
Download the source code
Gitee.com/zhang_fuqin…