本文主要內容
1 . 通過註解的方式實現mybatis的使用
2. 通過xml的方式實現mybatis
下載路徑 https://download.csdn.net/download/weixin_43055096/11639767
前期準備
application.yml的配置(把properties改爲yml)
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: wcw
password: 1234
url: jdbc:mysql://localhost:3306/mybaits?serverTimezone=UTC
jpa:
show-sql: true
database: mysql
pom.xml (主要用到的依賴)
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
項目主要目錄!
Student (這裏可能需要大家引入lamble)
package com.wuge.mybatis.dataobject;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
@Data
public class Student {
@Id
private String id;
private String name;
private String date;
}
StudentMapper(主要的業務代碼)
package com.wuge.mybatis.mapper;
import com.wuge.mybatis.dataobject.Student;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public interface StudentMapper {
//插入操作
@Insert("insert into student(id,name,date) values(" +
"#{id , jdbcType=VARCHAR}, " +
"#{name , jdbcType= VARCHAR} " +
",#{date , jdbcType= VARCHAR})")
int insertByMap(Map<String,Object> map);
@Insert("insert into student(id,name,date) values(" +
"#{id , jdbcType=VARCHAR}, " +
"#{name , jdbcType= VARCHAR} " +
",#{date , jdbcType= VARCHAR})")
int insertByObject(Student student);
/**
* column 對應數據庫字段名稱 而 property 對應對象名稱
*/
@Select("select * from student where id = #{id}")
@Results({
@Result(column = "id",property ="id"),
@Result(column = "name",property ="name"),
@Result(column = "date",property ="date")
})
Student findByStudentId(String id);
@Update("update student set name = #{name} where id =#{id}")
int updateById(@Param("name") String name,
@Param("id") String id);
@Update("update student set name = #{name} where id =#{id}")
int updateByObject(Student student);
@Delete("delete from student where id = #{id}")
int delectStudent(String id);
}
這裏方法返回的都爲int 因爲結果都是返回成功的條數
MybatisApplication 啓動類
package com.wuge.mybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.wuge.mybatis.mapper")
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
這裏的@MapperScan後寫的是mapper的路徑
這裏我們可以開始我們的測試了(以下都爲測試的方法)
package com.wuge.mybatis.mapper;
import com.wuge.mybatis.dataobject.Student;
import com.wuge.mybatis.repostory.StudentRepostory;
import com.wuge.mybatis.repostory.StudentRepostoryTest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentMapperTest{
@Autowired
private StudentMapper studentMapper;
@Autowired
private StudentRepostory studentRepostory;
@Test
public void insertbymap() throws Exception{
Map<String,Object> map = new HashMap<>();
map.put("id","2");
map.put("name","Bob");
map.put("date","2017-1-1");
int result = studentMapper.insertByMap(map);
Assert.assertEquals(1,result);
}
@Test
public void insertByObject(){
Student student = new Student();
student.setId("3");
student.setName("Linux");
student.setDate("2019-9-7");
int result = studentMapper.insertByObject(student);
Assert.assertEquals(1,result);
}
@Test
public void findAll(){
List<Student> student = studentRepostory.findAll();
Assert.assertNotEquals(0,student.size());
}
@Test
public void findById(){
Student result = studentMapper.findByStudentId("3");
Assert.assertNotEquals(null,result);
}
@Test
public void updateById(){
int reslut = studentMapper.updateById("boger","1");
Assert.assertEquals(1,reslut);
}
@Test
public void updateByObject(){
Student student = new Student();
student.setId("3");
student.setName("zhoujielun");
student.setDate("2019-9-7");
int result = studentMapper.updateByObject(student);
Assert.assertEquals(1,result);
}
@Test
public void deleteFromstudent(){
int result = studentMapper.delectStudent("1");
Assert.assertEquals(1,result);
}
@Test
public void selectBystudentXml(){
Student student = studentMapper.selectByStudent("2");
Assert.assertNotEquals(null,student);
}
}
xml 的使用方法
建立項目目錄(主要看mapper)
在yml添加配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: wcw
password: 1234
url: jdbc:mysql://localhost:3306/mybaits?serverTimezone=UTC
jpa:
show-sql: true
database: mysql
logging:
level:
com.wuge.mybatis.mapper: trace
# 獲取classpath下的所有xml文件
mybatis:
mapper-locations: classpath:mapper/*.xml
StudentMapper.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.wuge.mybatis.mapper.StudentMapper">
// 返回的結果 column爲數據庫字段 而property爲映射到實體類的字段
// type爲返回的類型
<resultMap id="BaseResultMap" type="com.wuge.mybatis.dataobject.Student">
<id column="id" property="id" jdbcType="VARCHAR"/>
<id column="name" property="name" jdbcType="VARCHAR"/>
<id column="date" property="date" jdbcType="VARCHAR"/>
</resultMap>
//parameterType 入參類型
<select id="selectByStudent" resultMap="BaseResultMap" parameterType="java.lang.String">
select * from student where
id = #{id , jdbcType = VARCHAR}
</select>
</mapper>
最後我們debug一下
可以看出結果是沒問題的