一、环境准备:
1. Windows 10
2. Maven 3.X
3. InterlliJ IDEA 2019
4. JDK 13(JDK1.8以上)
二、项目结构图:
三、详细代码部分:
1. Maven的pom.xml文件
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.study.module</groupId> <artifactId>mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mybatis</name> <description>Demo project for Spring Boot: ORM Framework Mybatis</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!--MySQL数据库连接--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--阿里巴巴连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.22</version> </dependency> <!--实体类:Lombok--> <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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!--swagger2 RESTful API接口文档--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2 application.yaml 项目配置文件
server: port: 8089 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/zero?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: root password: root123456 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:/mapper/*Dao.xml typeAliasesPackage: com.study.module.mybatis.entity
3. Mapper文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <mapper namespace="com.study.module.mybatis.dao.UserDao"> 4 5 <resultMap type="com.study.module.mybatis.entity.User" > 6 <result property="id" column="id" jdbcType="INTEGER"/> 7 <result property="name" column="name" jdbcType="VARCHAR"/> 8 <result property="password" column="password" jdbcType="VARCHAR"/> 9 </resultMap> 10 11 <!--查询单个--> 12 <select > 13 select 14 id, name, password 15 from zero.user 16 where id = #{id} 17 </select> 18 19 <!--查询指定行数据--> 20 <select > 21 select 22 id, name, password 23 from zero.user 24 limit #{offset}, #{limit} 25 </select> 26 27 <!--通过实体作为筛选条件查询--> 28 <select > 29 select 30 id, name, password 31 from zero.user 32 <where> 33 <if test="id != null"> 34 and id = #{id} 35 </if> 36 <if test="name != null and name != ''"> 37 and name = #{name} 38 </if> 39 <if test="password != null and password != ''"> 40 and password = #{password} 41 </if> 42 </where> 43 </select> 44 45 <!--新增所有列--> 46 <insert > 47 insert into zero.user(name, password) 48 values (#{name}, #{password}) 49 </insert> 50 51 <!--通过主键修改数据--> 52 <update > 53 update zero.user 54 <set> 55 <if test="name != null and name != ''"> 56 name = #{name}, 57 </if> 58 <if test="password != null and password != ''"> 59 password = #{password}, 60 </if> 61 </set> 62 where id = #{id} 63 </update> 64 65 <!--通过主键删除--> 66 <delete > 67 delete from zero.user where id = #{id} 68 </delete> 69 70 </mapper>