springboot整合mybatis

springboot整合mybatis的相关配置

1、导入mybatis的依赖

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>2.1.1</version>
</dependency>

2、配置数据源

spring:
datasource:
  username: root
  password: 123456
  url: jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&useUnicode=true@characterEncoding=utf-8
  driver-class-name: com.mysql.jdbc.Driver

3、测试类

package com.llxazy;

import com.alibaba.druid.pool.DruidDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@SpringBootTest
class DataApplicationTests {

   @Autowired
   DataSource dataSource;

   @Test
   void contextLoads() throws SQLException {
       System.out.println(dataSource.getClass());
       Connection connection = dataSource.getConnection();
       System.out.println(connection);
       System.out.println(connection.getMetaData().getURL());

       connection.close();
  }
}

4、创建实体类

package com.llxazy.entity;

import java.sql.Date;
import java.util.Objects;

public class Emp {
   private Integer empno;
   private String ename;
   private String job;
   private Integer mgr;
   private Date hiredate;
   private Double sal;
   private Double comm;
   private Integer deptno;

   //省略get/set/toString/构造方法

   @Override
   public boolean equals(Object o) {
       if (this == o) return true;
       if (!(o instanceof Emp)) return false;
       Emp emp = (Emp) o;
       return Objects.equals(empno, emp.empno) &&
               Objects.equals(ename, emp.ename) &&
               Objects.equals(job, emp.job) &&
               Objects.equals(mgr, emp.mgr) &&
               Objects.equals(hiredate, emp.hiredate) &&
               Objects.equals(sal, emp.sal) &&
               Objects.equals(comm, emp.comm) &&
               Objects.equals(deptno, emp.deptno);
  }

   @Override
   public int hashCode() {
       return Objects.hash(empno, ename, job, mgr, hiredate, sal, comm, deptno);
  }
}

5、配置Mapper接口类

package com.llxazy.mapper;

import com.llxazy.entity.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface EmpMapper {

   List<Emp> selectEmp();

   Emp selectEmpById(Integer empno);

   Integer addEmp(Emp emp);

   Integer updateEmp(Emp emp);

   Integer deleteEmp(Integer empno);
}

6、在resources下创建Emp.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.llxazy.mapper.EmpMapper">

   <select id="selectEmp" resultType="Emp">
    select * from emp
  </select>

   <select id="selectEmpById" resultType="Emp">
    select * from emp where empno = #{empno}
   </select>

   <insert id="addEmp" parameterType="Emp">
    insert into emp (empno,ename) values (#{empno},#{ename})
   </insert>

   <update id="updateEmp" parameterType="Emp">
    update emp set ename=#{ename} where empno = #{empno}
   </update>

   <delete id="deleteEmp" parameterType="int">
    delete from emp where empno = #{empno}
  </delete>
</mapper>

7、添加配置文件

spring:
datasource:
  username: root
  password: 123456
  url: jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&useUnicode=true@characterEncoding=utf-8
  driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
type-aliases-package: com.llxazy.entity

8、编写controller

package com.llxazy.contoller;

import com.llxazy.entity.Emp;
import com.llxazys.mapper.EmpMapper;
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 EmpController {
   @Autowired
   private EmpMapper empMapper;

   //选择全部用户
   @GetMapping("/selectEmp")
   public String selectEmp(){
       List<Emp> emps = empMapper.selectEmp();
       for (Emp Emp : emps) {
           System.out.println(Emp);
      }
       return "ok";
  }
   //根据id选择用户
   @GetMapping("/selectEmpById")
   public String selectEmpById(){
       Emp emp = empMapper.selectEmpById(1234);
       System.out.println(emp);
       return "ok";
  }
   //添加一个用户
   @GetMapping("/addEmp")
   public String addEmp(){
       empMapper.addEmp(new Emp(1234,"heheda"));
       return "ok";
  }
   //修改一个用户
   @GetMapping("/updateEmp")
   public String updateEmp(){
       empMapper.updateEmp(new Emp(1234,"heihei"));
       return "ok";
  }
   //根据id删除用户
   @GetMapping("/deleteEmp")
   public String deleteEmp(){
       empMapper.deleteEmp(1234);
       return "ok";
  }
}

9、测试即可

posted @ 2021-07-12 16:30  禾野牧村  阅读(415)  评论(0)    收藏  举报