mybatis与Springboot整合

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值