/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`springboot` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */;
USE `springboot`;
/*Table structure for table `t_user` */
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`password` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*Data for the table `t_user` */
insert into `t_user`(`id`,`username`,`password`) values (1,'zhangsan','123'),(2,'lisi','234');
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
使用select查询一下,出现结果就说明数据库的数据创建成功。
#datasource
spring:
datasource:
url: jdbc:mysql:///springboot
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
package com.example.springbootmybatis.domain;
public class User {
private int id;
private String username;
private String 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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
新建Interface类mapper.UserMapper,
package com.example.springbootmybatis.mapper;
import com.example.springbootmybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("select * from t_user") //接口纯注解方式
public List<User> findAll();
}
在test包下面新建SpringbootMybatisApplicationTests类,实例化接口类UserMapper,并使用findAll()方法将表中数据打印输出。
package com.example.springbootmybatis;
import com.example.springbootmybatis.domain.User;
import com.example.springbootmybatis.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SpringbootMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testFindAll() {
List<User> list=userMapper.findAll();
System.out.println(list);
}
}
然后点击代码左侧绿色箭头,运行testFindAll(),可以在控制台看到User表内容被成功输出了,配置成功。红字部分可以按照提示修改驱动就不报红了。
package com.example.springbootmybatis.mapper;
import com.example.springbootmybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
//主要是下面这部分
@Mapper
public interface UserXmlMapper {
public List<User> findAll(); //没有加注解了
}
因为没有加注解,要写配置文件和方法来进行映射,配置文件一般写在resources目录下,新建一个Directory命名为mapper,新建UserMapper.xml文件,键入如下代码,
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springbootmybatis.mapper.UserXmlMapper">
<select id="findAll" resultType="user">
select * from t_user;
select>
mapper>
注意namespace属性是复制UserXmlMapper的全路径名,Copy Reference,直接粘贴就可,粘错会报错的。因为把xml加载到spring中后,mybatis的初始化配置sqlsession时就会通过namespace+id
的方式找到对应的sql进行执行,mybatis会自动匹配xml与mapper接口的对应关系,需要文件名称一致。resultType
此处用了user作别名,等会儿需要配置一下。
然后去配置application.xml文件,使用mybatis.type-aliases-package属性把User配置进去。添加如下代码:
#mybatis
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml #mapper映射文件路径
type-aliases-package: com.example.springbootmybatis.domain
#config-location: #指定mybatis的核心配置文件
然后去配置SpringbootMybatisApplicationTests文件,添加如下代码,
@Autowired
private UserMapper userXmlMapper;
@Test
public void testFindAll2() {
List<User> list=userXmlMapper.findAll();
System.out.println(list);
}