Spring-Boot学习(3)-----Spring-Boot整合mybatis

为了更上技术的革新,近期打算学习下spring-Boot跟Spring-Cloud。下面将记录下自己学习过程。Spring-boot系列学习源代码放在https://github.com/wenbo2018/spring-boot-learning,有需要的可以自行下载。

spring-boot整合mybatis是非常方便而且简单的,没有以往那么多配置文件,下面记录详细过程。

1.maven依赖

 <parent>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-parentartifactId>
      <version>1.4.0.RELEASEversion>
  parent>

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
    <groupId>org.mybatis.spring.bootgroupId>
    <artifactId>mybatis-spring-boot-starterartifactId>
    <version>1.2.0version>
dependency>
<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <scope>runtimescope>
dependency>

2.application.properties配置文件

配置数据源和mybatis

#dataSource
spring.datasource.url=jdbc:mysql://localhost:3306/spring
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.profiles.active=dev

## Mybatis 配置
#实体类包
mybatis.typeAliasesPackage=com.github.wenbo2018.dto
#xml映射文件所在位置,通常weichatresources/mapper/
mybatis.mapperLocations=classpath:mapper/*.xml

3.mybatis配置

UserDao.xml配置



<mapper namespace="com.github.wenbo2018.dao.UserDao">

    <resultMap id="BaseResultMap" type="com.github.wenbo2018.dto.User">
        <result column="Id" property="id" jdbcType="INTEGER" />
        <result column="UserName" property="username" jdbcType="VARCHAR" />
        <result column="PassWord" property="password" jdbcType="VARCHAR" />
        <result column="Email" property="email" jdbcType="VARCHAR" />
        <result column="Au" property="au" jdbcType="INTEGER" />
    resultMap>


    <insert id="add" parameterType="com.github.wenbo2018.dto.User" >
        insert into User (
        UserName,
        PassWord,
        Au,Email,Status
        )
        values (
        #{username},
        #{password},
        #{au},#{email},#{status}
        )
    insert>

    <select id="list"  resultMap="BaseResultMap">
        SELECT * FROM User
    select>

    <delete id="delete" parameterType="int">
        DELETE FROM User WHERE Id=#{id}
    delete>

    <select id="load" parameterType="int" resultMap="BaseResultMap">
        SELECT * FROM User WHERE Id=#{id}
    select>

    <update id="update" parameterType="com.github.wenbo2018.dto.User">
        UPDATE  User SET  UserName=#{username}, Password=#{password},Au=#{au},Email=#{email}
    update>

    <select id="queryByUserNameAndPassWord"  resultMap="BaseResultMap">
        SELECT *  FROM User WHERE UserName=#{0} and  PassWord=#{1}
    select>

    <select id="loadByUserName" parameterType="java.lang.String" resultMap="BaseResultMap">
        SELECT * FROM User WHERE  UserName=#{username}
    select>
mapper>
public interface UserDao {


    public void add(User user);

    public User load(int userId);

    public User loadByUserName(String username);

    public void delete(int userId);

    public void update(User user);

    List list();

    User queryByUserNameAndPassWord(String username, String password);

}

实体类User


package com.github.wenbo2018.dto;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * Created by shenwenbo on 2017/4/14.
 */
@Getter
@Setter
public class User implements Serializable {

    private static final long serialVersionUID = -7091478494529379221L;

    private int id;
    private String username;
    private String password;
    private int au;
    private String email;

    private int status;

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

@Getter @Setter可以自动构造get set方法,只需要依赖lombok即可。

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.12.6version>
        dependency>

Application.class配置

@SpringBootApplication
@MapperScan("com.github.wenbo2018.dao")
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication app = new SpringApplication(Application.class);
        app.run(args);
    }
}

@MapperScan(“com.github.wenbo2018.dao”)用户扫描mapper接口,com.github.wenbo2018.dao为mapper接口所在包,spring-boot会自动将其注入到ioc中。

你可能感兴趣的:(spring-boot)