提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
一、基于注解
建一个User类,字段名和数据库保持一致
//user
package com.itheima.bootmybatis.domain;
public class User {
private int id;
private String username;
private String password;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
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;
}
public void setPassword(String password) {
this.password = password;
}
}
//UserMapper
package com.itheima.bootmybatis.mapper;
import com.itheima.bootmybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
@Select("select * from t_user")
public List<User> findAll();
}
//test测试,BootMybatisApplicationTests
package com.itheima.bootmybatis;
import com.itheima.bootmybatis.domain.User;
import com.itheima.bootmybatis.mapper.UserMapper;
// import com.itheima.bootmybatis.mapper.UserXmlMapper;
import org.junit.jupiter.api.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.List;
@RunWith(SpringRunner.class)
@SpringBootTest
class BootMybatisApplicationTests {
@Autowired
public UserMapper userMapper;
@Test
public void testUserfindall() {
List<User> all = userMapper.findAll();
System.out.println(all);
}
}
#JDK8以下com.mysql.jdbc.Driver
#8以上com.mysql.cj.jdbc.Driver
spring:
datasource:
url: jdbc:mysql:///ssm?serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
二、基于注解的整合mybatis
//在mapper下创建一个userxmlmapper的接口
@Mapper
public interface UserxmlMapper {
public List<User> findall1();
}
<?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>
<select id="findall1" resultType="user">
select * from t_user
</select>
</mapper>
#application.yaml
spring:
datasource:
url: jdbc:mysql:///ssm?serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com/itheima/bootmybatis/domain
package com.itheima.bootmybatis;
import com.itheima.bootmybatis.domain.User;
import com.itheima.bootmybatis.mapper.UserMapper;
// import com.itheima.bootmybatis.mapper.UserXmlMapper;
import com.itheima.bootmybatis.mapper.UserxmlMapper;
import org.junit.jupiter.api.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.List;
@RunWith(SpringRunner.class)
@SpringBootTest
class BootMybatisApplicationTests {
@Autowired
public UserMapper userMapper;
@Autowired
public UserxmlMapper userxmlMapper;
@Test
public void testUserfindall() {
List<User> all = userMapper.findAll();
System.out.println(all);
}
@Test
public void testUserfindall1() {
List<User> all = userxmlMapper.findall1();
System.out.println(all);
}
}
总结
基于注解比较方便