标签:efault err 目录 bind 技术 load table 配置信息 namespace
SpringBoot整合Mybatis主要分为以下几个步骤:
1、添加Mybatis的起步依赖(pom.xml)
2、添加数据库驱动坐标(pom.xml)
3.添加数据库连接信息(application.properties)
4、创建user表
5、创建实体Bean
6、编写Mapper接口,可理解为dao
7、配置Mapper接口对应的Mapper映射文件,可理解为dao的方法要执行的sql语句
8、在application.properties中添加Mybatis的相关信息
9、编写Controller
10、启动SpringBoot的引导类尽心测试
具体代码如下:
pom.xml中与整合Mybatis相关的配置如下:
<!--mybatis起步依赖-->
<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>
<!--不需要填写版本号,因为在父依赖里已经给了判断-->
<!--<version>5.1.39</version>-->
</dependency>
application.properties中的配置信息如下:
#数据库连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root@123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#springboot集成mybatis
#pojo扫描
mybatis.type-aliases-package=come.itheima.domain
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
建表语句以及插入两条测试数据,如下:
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
insert into user values(1,‘zhangsan‘,‘123‘,‘张三‘);
insert into user values(2,‘lisi‘,‘123‘,‘李四‘);
user实体的内容如下:
package com.itheima.domain;
public class User {
private int id;
private String username;
private String password;
private String name;
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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username=‘" + username + ‘\‘‘ +
", password=‘" + password + ‘\‘‘ +
", name=‘" + name + ‘\‘‘ +
‘}‘;
}
}
Mapper接口内容如下:
package com.itheima.mapper;
import com.itheima.domain.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
public List<User> queryUserList();
}
UserMapper.xml接口与执行sql的映射文件内容如下:
<?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.itheima.mapper.UserMapper">
<select id="queryUserList" resultType="com.itheima.domain.User">
select * from user
</select>
</mapper>
controller代码内容如下:
package com.itheima.controller;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class MybatisController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/query")
@ResponseBody
public List<User> queryUserList(){
List<User> users = userMapper.queryUserList();
return users;
}
}
默认生成的引导类内容如下:
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
整体目录结构如下图:
标签:efault err 目录 bind 技术 load table 配置信息 namespace
原文地址:https://www.cnblogs.com/xiao1572662/p/11875366.html