SpringBoot 札记 整合MyBatis (重头戏)

要求:SpringBoot项目

一:导入mybatis依赖

<dependency>
    <groupId>org.mybatis.spring.bootgroupId>
    <artifactId>mybatis-spring-boot-starterartifactId>
    <version>2.1.1version>
dependency>

二:配置数据库连接信息 application.yml

# 数据库驱动
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
    username: root
    password: 你的密码

这里配置完数据库 测试连接成功 可参考 测试连接成功与否

三:创建实体类 User

public class User {
    private Integer id;
    private String name;
    private String pwd;

    public User() {
    }

    public User(Integer id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    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 String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }
}

四:创建Mapper 接口 以及 对应的Mapper映射文件

UserMapper.java

import com.csnz.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;

@Mapper//表示这个类是mybatis的mapper类
@Repository
public interface UserMapper {
    //查询全部用户
    List<User> getUserList();
    //根据id查询用户
    User findById(@Param("id") int id);
    //增加一个用户
    int addUser(User user);
    //根据id删除一个用户
    int deleteById(@Param("id") int id);
    //根据id更新一个用户
    int updateById(User user);
}

UserMapper.xml


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.csnz.mapper.UserMapper">


    
    <select id="getUserList" resultType="User">
        select * from user;
    select>

    
    <select id="findById" parameterType="Integer" resultType="User">
        select * from user where id = #{id};
    select>

    
    <insert id="addUser" parameterType="User">
        insert into user (`id`,`name`,`pwd`) values(#{id},#{name},#{pwd});
    insert>

    
    <delete id="deleteById" parameterType="Integer">
        delete from user where id = #{id};
    delete>

    
    <update id="updateById" parameterType="User">
        update user set `name` = #{name},`pwd`=#{pwd} where id = #{id};
    update>
mapper>

五:在springboot配置文件中 整合mybatis 配置别名和扫描mapper映射文件 application.yml

# 整合Mybatis
mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml #mapper文件对应的位置 classpath指的是resources目录
  type-aliases-package: com.csnz.pojo # 给实体类起别名

六:maven配置资源过滤问题 pom.xml中


        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                    <include>**/*.ymlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
        resources>

七:创建dao对应的service接口 以及 对应的service实现类

UserService.java

import com.csnz.pojo.User;

import java.util.List;

public interface UserService {
    //查询全部用户
    List<User> getUserList();
    //根据id查询用户
    User findById( int id);
    //增加一个用户
    int addUser(User user);
    //根据id删除一个用户
    int deleteById( int id);
    //根据id更新一个用户
    int updateById(User user);
}

UserServiceImpl

import com.csnz.mapper.UserMapper;
import com.csnz.pojo.User;
import com.csnz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getUserList() {
        return userMapper.getUserList();
    }

    @Override
    public User findById(int id) {
        return userMapper.findById(id);
    }

    @Override
    public int addUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public int deleteById(int id) {
        return userMapper.deleteById(id);
    }

    @Override
    public int updateById(User user {
        return userMapper.updateById(user);
    }
}

八:编写controller进行测试 UserController

import com.csnz.pojo.User;
import com.csnz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    //查询全部用户
    @GetMapping("/getUserList")
    public List<User> getUserList(){
        List<User> userList = userService.getUserList();
        return userList;
    }
    //根据id查询用户
    @GetMapping("/findById/{id}")
    public User findById(@PathVariable("id") Integer id){
        User user = userService.findById(id);
        return user;
    }
    //增加一个用户
    @GetMapping("/addUser")
    public String addUser(){
        int i = userService.addUser(new User(6, "张三丰", "000"));
        return i>0?"Add oK":"add Error";
    }
    //根据id删除一个用户
    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") Integer id){
        int i = userService.deleteById(id);
        return i>0?"delete OK":"delete Error";
    }
    //根据id更新一个用户
    @GetMapping("/updateById/{id}")
    public String updateById(@PathVariable("id") Integer id){
        User user = new User(id, "修改的名字", "修改的密码");
        int i = userService.updateById(user);
        return i>0?"update OK":"update Error";
    }
}

你可能感兴趣的:(SpringBoot,mybatis,springboot)