springboot整合Mybatis
来源:互联网 发布:海森矩阵的作用 编辑:程序博客网 时间:2024/05/17 02:51
pom.xml
中引入依赖
- 这里用到spring-boot-starter基础和spring-boot-starter-test用来做单元测试验证数据访问
- 引入连接mysql的必要依赖mysql-connector-java
- 引入整合MyBatis的核心依赖mybatis-spring-boot-starter
- 这里不引入spring-boot-starter-jdbc依赖,是由于mybatis-spring-boot-starter中已经包含了此依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.21</version></dependency></dependencies>
application.properties
中配置mysql的连接配置#springboot整合mybatis框架#加载Mybatis配置文件 相当于spring扫描mybatis.mapper-locations=classpath:mapper/*Mapper.xml#mybatis.config-location=classpath:mapper/config/sqlMapperConfig.xml#定义别名mybatis.type-aliases-package=com.example.demo.pojo#数据源spring.datasource.url=jdbc:mysql://101.200.230.153:3306/user?useUnicode=true&characterEncoding=UTF-8spring.datasource.username=rootspring.datasource.password=123spring.datasource.driver-class-name=com.mysql.jdbc.Driver#配置端口server.port=8082编写User实体类
public class User { private Long id; private String name; private Integer age; //省略get/set编写Mapper接口类
@Mapperpublic interface UserMapper { //查询所有用户 List<User> findAll();}编写UserService
public interface UserService { //查询所有用户 List<User> findAll();}编写UserService实现类
@Servicepublic class UserServiceImpl implements UserService { //注入mapper代理接口对象 @Autowired private UserMapper userMapper; @Override public List<User> findAll() { return userMapper.findAll(); }}编写Controller
@RestControllerpublic class UserController { //注入Service服务对象 @Autowired private UserService userService; /** * 查询用户 */ @RequestMapping("/user") public List<User> findAll(){ return userService.findAll(); }}UserMapper类
<?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.example.demo.mapper.UserMapper" ><!--namespace跟Mapper路径一致-->
<select id="findAll" resultType="com.example.demo.pojo.User"> select * FROM user </select></mapper>
运行springboot项目出现下面的页面 这样简单的springboot整合mybatis就完成了
阅读全文
0 0
- SpringBoot学习:SpringBoot整合mybatis
- SpringBoot和Mybatis整合
- springboot 整合mybatis
- springboot+mybatis+dubbo整合
- SpringBoot整合MyBatis
- SpringBoot整合Mybatis
- springBoot整合mybatis
- springboot与mybatis整合
- springboot和mybatis整合
- Springboot整合Mysql+Mybatis
- springboot、mybatis、activemq整合
- SpringBoot整合MyBatis
- SpringBoot 整合mybatis
- SpringBoot + Mybatis 整合demo
- springboot整合mybatis
- SpringBoot-整合MyBatis
- Springboot 整合 Mybatis
- SpringBoot-09整合MyBatis
- 【windows】【编译器版本查】教你查看VS IDE中编译器版本号
- STL常用容器用法之——Map和multimap
- 爬取智联招聘(面向对象)
- 对数学建模论文的一些建议
- relative 和absolute
- springboot整合Mybatis
- vertical-align应用
- hdu 6180 Schedule 贪心
- python的官方下载, 安装, IDE工具介绍,HelloWord
- [js高手之路]原型式继承与寄生式继承
- __solts__的使用
- rcnn网络学习资料整理
- 算法复习:第一个只出现一次的字符
- 什么是无后效性?