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.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.4.0.RELEASE</version>
  </parent>
<!-- Spring Boot web 依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.2.0</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</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配置

<?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.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<User> 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.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.12.6</version>
        </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中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值