springboot整合mybatis的xml形式

前言

mybatis开发过程中用到的还是比较多的,之前我用的大多都是注解形式,xml用的不多,所以就用了点时间吧xml格式的也看了下,做了总结在这里写下。

开始工作

引入jar包

		
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        

        
            mysql
            mysql-connector-java
        

配置文件

#驱动名称
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#url地址
spring.datasource.url=jdbc:mysql://localhost:3306/poetry?useUnicode=true&characterEncoding=utf-8&&serverTimezone=GMT%2B8
#用户名
spring.datasource.username=root
#密码
spring.datasource.password=yu123467.

#扫描实体类路径
mybatis.type-aliases-package=com.xue.mybitsdemo.entity
#映射文件路径及名称
mybatis.mapper-locations:classpath:mapper/*Mapper.xml
#mybats配置文件
mybatis.config-location:classpath:config/mybits-config.xml

测试用表建表语局

CREATE TABLE `student` (
	`s_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '主键id',
	`s_name` VARCHAR(50) NOT NULL DEFAULT '0' COMMENT '学生姓名',
	`s_age` INT(11) NOT NULL DEFAULT '0' COMMENT '学生年龄',
	`s_sex` VARCHAR(50) NOT NULL DEFAULT '0' COMMENT '学生性别',
	PRIMARY KEY (`s_id`)
)
COMMENT='学生信息表'
ENGINE=InnoDB
;

目录结构

-- main
	--java
		--com.xue.mybitsdemo
			--dao
				--StudentMapper.class
			--entity
				--StudentEntity.class
			--MybitsdemoApplication.class	
	--resource
		--config
			mybatis-config.xml
		--mapper
			StudentMapper.xml 
		--application.properties

xml格式

mybits-config.xml文件内容

可设置项有很多,更多的戳这里




    
        
        
    

Mapper开发

package com.xue.mybitsdemo.dao;

import com.xue.mybitsdemo.entity.StudentEntity;

import java.util.List;

public interface StudentMapper {
    //根据id获取一条数据
    StudentEntity getOne(Integer id);

    //根据多条件获取一条数据
    StudentEntity getOne2(StudentEntity studentEntity);

    //获取全部数据
    List<StudentEntity> getAll();

    //插入一条数据
    void insert(StudentEntity studentEntity);

    //删除一条数据
    void deleteOne(Integer id);

    //修改一条数据
    void update(StudentEntity studentEntity);

}

这些接口需要在映射文件中一一实现,可以在类文件上添加@Mapper注解,很多Mapper的话有点麻烦,推荐直接在启动类那添加。启动的时候,就去扫描,自动注入了

@SpringBootApplication
@MapperScan(basePackages= {"com.xue.mybitsdemo.dao"})
public class MybitsdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybitsdemoApplication.class, args);
    }

}

实体类文件

package com.xue.mybitsdemo.entity;

public class StudentEntity {
    private Integer id;
    private String name;
    private Integer age;
    private String sex;

    public StudentEntity(Integer id, String name, Integer age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public StudentEntity(String name, Integer age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "StudentEntity{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}

添加Student的映射文件

<?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.xue.mybitsdemo.dao.StudentMapper">
    <resultMap id="studentMap" type="StudentEntity">
        <id column="s_id" property="id" jdbcType="BIGINT"/>
        <result column="s_name" property="name"/>
        <result column="s_age" property="age"/>
        <result column="s_sex" property="sex"/>
    </resultMap>

    <!--    <insert id="insert" useGeneratedKeys="true"-->
    <!--            keyProperty="id" parameterType="StudentEntity" >-->
    <!--            insert into student (s_name,s_age,s_sex)-->
    <!--            values (#{name},#{age},#{sex})-->
    <!--    </insert>-->
    <insert id="insert" parameterType="StudentEntity">
            insert into student (s_name,s_age,s_sex)
            values (#{name},#{age},#{sex})
    </insert>


    <select id="getAll" resultMap="studentMap">
        select s_id,s_name,s_age,s_sex from student
    </select>

    <select id="getOne" parameterType="Integer" resultMap="studentMap">
        select s_id,s_name,s_age,s_sex from student where s_id = #{id}
    </select>

    <select id="getOne2" parameterType="StudentEntity" resultMap="studentMap">
        select s_id,s_name,s_age,s_sex from student
        <where>
            <if test="id != null">
                s_id=#{id}
            </if>
            <if test="name != null">
                AND s_name=#{name}
            </if>
            <if test="age != null">
                AND s_age=#{age}
            </if>
            <if test="sex != null">
                AND s_sex=#{sex}
            </if>
        </where>
    </select>

    <delete id="deleteOne" parameterType="Integer">
        delete from  student where s_id = #{id}
    </delete>

    <!--    <update id="update" parameterType="StudentEntity">-->
    <!--        update student set s_name = #{name},s_age = #{age},s_sex = #{sex} where s_id=#{id}-->
    <!--    </update>-->

    <update id="update" parameterType="StudentEntity">
        update student
        <set>
            <if test="name != null">s_name=#{name},</if>
            <if test="age != null">s_age=#{age},</if>
            <if test="sex != null">s_sex=#{sex}</if>
        </set>
        where s_id=#{id}
    </update>
</mapper>
xml详细解释

resultMap 自定义返回结果。
主要用于转换字段,当数据库的字段和实体类的不一致的时候。其中column属性是数据库字段,property属性是实体类的字段。数据库里面的是“s_name”字段,实体类的是“name”字段。当然了,还有更简单的,那就是数据库查询的时候直接 select s_name as name from student。 用resultMap 主要是为了规范