SpringBoot整合MyBatis
来源:互联网 发布:h5活动报名系统源码 编辑:程序博客网 时间:2024/06/10 15:57
- 数据库配置文件
SpringBoot将数据库的配置写在application.properties中,启动应用程序时会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中
application.properties:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/resturant?useUnicode=true&characterEncoding=UTF-8spring.datasource.username=rootspring.datasource.password=daydayup01spring.datasource.driver-class-name=com.mysql.jdbc.Driver
可以省略spring.datasource.driver-class-name,因为SpringBoot会根据url推断出相应的drivEClassName
Model:
/** * @author hetiantian * 实体类 */public class Owner { private Integer id; private String name; public Owner(Integer id, String name) { this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Owner{" + "id=" + id + ", name='" + name + '\'' + '}'; }}
Dao层:
/** * @aurhor hetiantian * 数据化持久层接口 */@Repository@Mapperpublic interface OwnerMapper { /** * 查询出所有的owner * @return * */ @Select("select * from owner") List<Owner> queryAllOwners();}
SpringBoot是尽量减少配置,所以这里没有使用xml配置文件
@Mapper和@Repository既说明它是mapper接口,又是数据化持久层接口
Service层:
@Servicepublic class OwnerService { @Autowired private OwnerMapper ownerMapper; public List<Owner> queryAllOwners() { List<Owner> ownerList = ownerMapper.queryAllOwners(); if (ownerList == null || ownerList.size() == 0) { throw new MyException("没有查询到任何信息"); } return ownerList; }}
Controller层:
/** * @author hetiantian * 控制层 */@Controllerpublic class OwnerAction { @Autowired private OwnerService ownerService; @RequestMapping("/queryAllOwners") @ResponseBody public List<Owner> queryAllOwners() { return ownerService.queryAllOwners(); }}
附:pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.squpt.springboot</groupId> <artifactId>springbootdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springbootdemo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--mybatis整合springboot的依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--mysql驱动依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> <version>1.3.4.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
附项目完整地址:https://github.com/TiantianUpup/SpringBootDemo
阅读全文
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
- Full Tank? POJ
- U3D 发布相关
- CI框架最新加密解密类用法测试
- Making the text surround the picture
- ROC曲线
- SpringBoot整合MyBatis
- sdut 双向链表
- 基于C++全局变量的声明与定义的详解
- CodeForces
- S5PV210开发板刷机(SD卡uboot、串口+USB-OTG刷机方法)
- HDU 1217 Arbitrage(最短路判断负环)
- C++ STL vector 用法详解
- Linux线程简述
- vs2013 批处理程序自动编译实例