1 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2 application.yml
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ligeyu?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=ligeyu
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.typeAliasesPackage=com.hangzhou.binghe.firstspringboot.model
mybatis.mapperLocations=classpath:mapper/*.xml
logging.level.com.shizhao.project.springdemo:DEBUG
server.port=8282
3 dao
package com.hangzhou.binghe.firstspringboot.dao;
import com.hangzhou.binghe.firstspringboot.model.Person;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface PersonDao {
List<Person> getPerson(@Param("person") Person person);
void addAllPerson(@Param("persons") List<Person> persons);
void updatePerson(Person person);
}
4 .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.hangzhou.binghe.firstspringboot.dao.PersonDao">
<resultMap id="userMap" type="com.hangzhou.binghe.firstspringboot.model.Person">
<id property="id" column="id" javaType="long"/>
<result property="name" column="user_name" />
<result property="age" column="user_age"/>
<result property="height" column="user_height"/>
</resultMap>
<sql id="sqlOne">
id,user_name,user_age,user_height from person
</sql>
<select id="getPerson" parameterType="com.hangzhou.binghe.firstspringboot.model.Person" resultMap="userMap" >
select <include refid="sqlOne"/>
<where>
<if test="person.id != null" >
and id = #{person.id}
</if>
<if test="person.name != null and person.name != ''">
and user_name like concat(concat("%",#{person.name},"%"))
</if>
</where>
</select>
<insert id="addAllPerson" parameterType="java.util.List" >
insert into person (user_name,user_age,user_height) values
<foreach collection="persons" item="person" separator="," index="index">
(#{person.name},#{person.age},#{person.height})
</foreach>
</insert>
<update id="updatePerson" parameterType="com.hangzhou.binghe.firstspringboot.model.Person">
update person
<trim prefix="set" suffix="where id = #{id}" suffixOverrides=",">
<if test="name != null and name != ''" >
user_name = #{name},
</if>
<if test="age != null " >
user_age = #{age},
</if>
<if test="height != null " >
user_height = #{height},
</if>
</trim>
</update>
</mapper>
位置:resources ->mapper ->personMapper.xml