项目结构:
1.添加依赖
<?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> <groupId>com.wk</groupId> <artifactId>mango</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mango</name> <description>mango</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.13</version> <relativePath/> <!-- lookup parent from repository --> </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.springframework.boot</groupId>--> <!-- <artifactId>spring-boot-starter-actuator</artifactId>--> <!-- </dependency>--> <!-- <dependency>--> <!-- <groupId>org.springframework.boot</groupId>--> <!-- <artifactId>spring-boot-starter-data-jpa</artifactId>--> <!-- </dependency>--> <!-- <dependency>--> <!-- <groupId>org.springframework.boot</groupId>--> <!-- <artifactId>spring-boot-starter-security</artifactId>--> <!-- </dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- mysql 驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <!-- <dependency>--> <!-- <groupId>org.springframework.boot</groupId>--> <!-- <artifactId>spring-boot-devtools</artifactId>--> <!-- <scope>runtime</scope>--> <!-- <optional>true</optional>--> <!-- </dependency>--> <!-- <dependency>--> <!-- <groupId>org.projectlombok</groupId>--> <!-- <artifactId>lombok</artifactId>--> <!-- <optional>true</optional>--> <!-- </dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- <dependency>--> <!-- <groupId>org.springframework.security</groupId>--> <!-- <artifactId>spring-security-test</artifactId>--> <!-- <scope>test</scope>--> <!-- </dependency>--> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
2.修改配置文件
server: port: 8001 spring: mvc: pathmatch: matching-strategy: ant_path_matcher datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mango?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai username: root password: soft mybatis: mapper-locations: classpath:mapper/*.xml //mapper类对应的 xml文件地址 type-aliases-package: com.wk.mango.model //实体类所在包
3.启动类增加 Mapper扫描启动项
@SpringBootApplication() @MapperScan("com.wk.mango.mapper") //mapper类所在包路径 public class MangoApplication { public static void main(String[] args) { SpringApplication.run(MangoApplication.class, args); } }
4.一个完整的访问
实体类
package com.wk.mango.model; import java.util.Date; import lombok.Data; /** * 用户管理 * table sys_user * * @author wangkun * @date 2023-07-19 19:46:32 */ @Data public class SysUser { /** * 编号 */ private Long 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 Byte status; /** * 机构ID */ private Long deptId; /** * 创建人 */ private String createBy; /** * 创建时间 */ private Date createTime; /** * 更新人 */ private String lastUpdateBy; /** * 更新时间 */ private Date lastUpdateTime; /** * 是否删除 -1:已删除 0:正常 */ private Byte delFlag; }
Mapper类
package com.wk.mango.mapper; import com.wk.mango.model.SysUser; import java.util.List; public interface SysUserMapper { int deleteByPrimaryKey(Long id); int insert(SysUser row); int insertSelective(SysUser row); SysUser selectByPrimaryKey(Long id); int updateByPrimaryKeySelective(SysUser row); int updateByPrimaryKey(SysUser row); /** * 查询全部 */ List<SysUser> findAll(); }
Mapper类所对应的 xml
<?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.wk.mango.mapper.SysUserMapper"> <resultMap id="BaseResultMap" type="com.wk.mango.model.SysUser"> <[email protected] 2023-07-19 19:46:32--> <!--@Table sys_user--> <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.wk.mango.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.wk.mango.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.wk.mango.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.wk.mango.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"></include> from sys_user </select> </mapper>
service类
package com.wk.mango.service; import com.wk.mango.model.SysUser; import java.util.List; /** * @author wangkun * @date 2023/7/19 */ public interface SysUserService { /** * 查找所有用户 * @return */ List<SysUser> findAll(); }
service实现类
package com.wk.mango.impl; import com.wk.mango.mapper.SysUserMapper; import com.wk.mango.model.SysUser; import com.wk.mango.service.SysUserService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; /** * @author wangkun * @date 2023/7/19 */ @Service public class SysUserServiceImpl implements SysUserService { @Resource private SysUserMapper sysUserMapper; @Override public List<SysUser> findAll() { return sysUserMapper.findAll(); } }
controller控制器
package com.wk.mango.controller; import com.wk.mango.service.SysUserService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author wangkun * @date 2023/7/19 */ @RestController @RequestMapping("user") public class SysUserController { @Resource private SysUserService sysUserService; @GetMapping("/findAll") public Object findAll(){ return sysUserService.findAll(); } }
启动项目后访问:http://localhost:8001/user/findAll
显示如下图:
mybatis整合完成
标签:VARCHAR,springboot,--,boot,private,整合,jdbcType,mybatis,id From: https://www.cnblogs.com/wang1204/p/17566711.html