SpringBoot整合MyBatis
2017-10-07 18:14
288 查看
- 数据库配置文件
SpringBoot将数据库的配置写在application.properties中,启动应用程序时会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中
application.properties:
可以省略spring.datasource.driver-class-name,因为SpringBoot会根据url推断出相应的drivEClassName
Model:
Dao层:
SpringBoot是尽量减少配置,所以这里没有使用xml配置文件
@Mapper和@Repository既说明它是mapper接口,又是数据化持久层接口
Service层:
Controller层:
附:pom.xml
附项目完整地址:https://github.com/TiantianUpup/SpringBootDemo
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-8 spring.datasource.username=root spring.datasource.password=daydayup01 spring.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 @Mapper public interface OwnerMapper { /** * 查询出所有的owner * @return * */ @Select("select * from owner") List<Owner> queryAllOwners(); }
SpringBoot是尽量减少配置,所以这里没有使用xml配置文件
@Mapper和@Repository既说明它是mapper接口,又是数据化持久层接口
Service层:
@Service public 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 * 控制层 */ @Controller public 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
相关文章推荐
- SpringBoot和Mybatis整合结构 火推 03
- springBoot入门总结(五)整合Mybatis(注解方式)
- springboot、mybatis、thymeleaf、bootstrap整合框架
- Springboot整合mybatis出现无法注入mapper接口的问题处理
- springboot 整合mybatis 遇到的问题
- SpringBoot整合mybatis,shiro,redisjiyushujuku细粒度动态权限管理
- SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper实例详解
- springboot整合mybatis mapper注入时显示could not autowire的解决
- SpringBoot整合MyBatis
- maven多模块,springboot整合mybatis
- SpringBoot--整合Mybatis和Redis
- springboot整合mybatis(映射文件方式和注解方式)
- java鬼混笔记:springboot 9、springboot整合mybatis加上分页功能
- Springboot 整合 Mybatis 的完整 Web 案例
- springboot【13】数据访问之整合Mybatis
- SpringBoot 整合 MyBatis 及引入事务
- SpringBoot 整合 MyBatis 实现简单注解开发
- springboot与mybatis整合 实例详解(完美融合)
- SpringBoot(九)SpringBoot整合tk.mybatis
- 企业级 SpringBoot 教程 (六)springboot整合mybatis