导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.18</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
在application中配置
spring.datasource.username=root
spring.datasource.password=****
spring.datasource.url=****
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.type-aliases-package=***
mybatis.mapper-locations=classpath:mapper/*.xml
添加实体类
@Data
public class Bigman implements Serializable {
private String name;
private String phone;
private String firm;
private String forword;
private String image;
private static final long serialVersionUID = 1L;
}
添加dao层
@Component
@Mapper
public interface BigmanDao {
int insert(Bigman record);
int insertSelective(Bigman record);
ArrayList<Bigman> select();
}
添加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.example.demo.dao.BigmanDao">
<resultMap id="BaseResultMap" type="com.example.demo.pojo.Bigman">
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
<result column="firm" jdbcType="VARCHAR" property="firm" />
<result column="forWord" jdbcType="VARCHAR" property="forword" />
</resultMap>
<insert id="insert" parameterType="com.example.demo.pojo.Bigman">
insert into bigman (`name`, phone, firm,
forWord)
values (#{name,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{firm,jdbcType=VARCHAR},
#{forword,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.example.demo.pojo.Bigman">
insert into bigman
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null">
`name`,
</if>
<if test="phone != null">
phone,
</if>
<if test="firm != null">
firm,
</if>
<if test="forword != null">
forWord,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="phone != null">
#{phone,jdbcType=VARCHAR},
</if>
<if test="firm != null">
#{firm,jdbcType=VARCHAR},
</if>
<if test="forword != null">
#{forword,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="select" resultMap="BaseResultMap">
select *from bigman
</select>
</mapper>
在controller层自动装配后使用即可