springboot集成mybatis
1、创建一个springboot项目
2、在pom.xml文件中添加mybatis的起步依赖
<!--mybatis起步依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
3、添加数据库驱动坐标,对应的版本不用导,应为对用的maven已经有对应的坐标
<!--MySQL连接驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
4、在application.properties文件中配置数据库连接信息(在此key值是不能乱写的不然springboot无法识别,得去下图所示的json文件中查找,找得到的便是可以用的key,不过一般的编译工具都有提示功能)
#数据库连接信息
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=12345678
5、在application.properties文件中配置配置mybatis的信息
#配置mybatis的信息 #扫描包 mybatis.type-aliases-package=com.springboot #加载mybatis映射文件 mybatis.mapper-locations=classpath:mapper/*Mapper.xml
至此springboot集成mybatis就完成了
6、编写测试代码
在src.main.java.com.springboot路径下新建domain包,在包中新建User类,
代码如下
package com.springboot.domain; public class User { private Integer id; private String username; private String password; private String name; @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", name='" + name + '\'' + '}'; } public Integer getId() { return id; } public void setId(Integer 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; } }
在src.main.resources路径下新建mapper包,在包中新建UserMapper.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.springboot.mapper.UserMapper"> <select id="queryUserList" resultType="user"> select * from user </select> </mapper>
在src.main.java.com.springboot路径下新建mapper包,在包中新建UserMapper接口,
代码如下
package com.springboot.mapper; import com.springboot.domain.User; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface UserMapper { public List<User> queryUserList(); }
在src.main.java.com.springboot路径下新建controller包,在包中新建MybatisController,
代码如下
package com.springboot.controller; import com.springboot.domain.User; import com.springboot.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; } }
user表的建表语句
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=10 default charset=utf8; insert into user values(1,'zhangsan','123','张三'); insert into user values(2,'lisi','123','李四');
7、开始测试
在浏览器中输入http://localhost:8080/query,结果如下图
则说明测试成功。