5.springboot整合mybatis(注解方式)
来源:互联网 发布:vivox7和荣耀8知乎 编辑:程序博客网 时间:2024/06/01 08:58
1.整合mybatis
有两种方式,使用官方提供的springboot-mybatis包,还有一种是通过mybatis-spring的方式
这里使用第一种方式
2.建立工程
(1)pom.xml
<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.tyf</groupId> <artifactId>test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>test</name> <url>http://maven.apache.org</url><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.2.RELEASE</version><relativePath/></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><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><!-- mybitas --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version></dependency><!-- mybitas --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.21</version><!-- mysql --></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><configuration><skip>true</skip></configuration></plugin></plugins></build></project>
(2)application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=123spring.datasource.driver-class-name=com.mysql.jdbc.Driverserver.port=1111
(3)实体类
package com.tyf.test;public class Accoun {/** * @return the accoun_id */public Integer getAccoun_id() {return accoun_id;}/** * @param accoun_id the accoun_id to set */public void setAccoun_id(Integer accoun_id) {this.accoun_id = accoun_id;}/** * @return the name */public String getName() {return name;}/** * @param name the name to set */public void setName(String name) {this.name = name;}/** * @return the pass */public String getPass() {return pass;}/** * @param pass the pass to set */public void setPass(String pass) {this.pass = pass;}/** * @return the login */public String getLogin() {return login;}/** * @param login the login to set */public void setLogin(String login) {this.login = login;}private Integer accoun_id;private String name;private String pass ;private String login;}
(4)dao
package com.tyf.test;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;@Mapperpublic interface AccounDao {@Select("select * from accoun where accoun_id = #{id}")Accoun findUserById(@Param("id")Integer id);}
(5)controller
package com.tyf.test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/accoun")public class AccounController {@AutowiredAccounDao accounDao;@RequestMapping(value="/getAccoun",method = RequestMethod.GET)@ResponseBodypublic String getAccoun(){Accoun accoun = (Accoun)accounDao.findUserById(5);return accoun.toString();}}
(6)app
package com.tyf.test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * Hello world! * */@SpringBootApplicationpublic class App { public static void main( String[] args ) { SpringApplication.run(App.class,args); }}
阅读全文
0 0
- 5.springboot整合mybatis(注解方式)
- springboot之整合mybatis-annotation(注解方式)
- springboot 注解方式整合mybatis (mysql数据库)
- 全注解方式整合spring+mybatis模拟springboot整合
- springboot整合mybatis 注解实现
- springboot整合redis,注解方式
- Springboot集成mybatis注解方式
- springboot整合mybatis方式一
- 关于springboot整合mybatis,做关联查询的两种方式(注解和配置文件)
- springboot整合mybatis xml方式配置SQL
- springboot整合mybatis的三种方式
- SpringMVC+MyBatis整合(注解方式)
- springboot项目使用注解法整合mybatis的具体步骤
- springboot 整合mybatis 基于注解开发 传入对象
- SpringBoot整合Mybatis的简单例子(注解版)
- SpringBoot学习:SpringBoot整合mybatis
- SpringBoot MyBatis注解
- SpringBoot和Mybatis整合
- Swift中获取随机数
- ssh 公钥私钥认证原理
- 观黑泽民的电影(一·)
- struts拦截器l
- Android性能优化之Bitmap的内存优化
- 5.springboot整合mybatis(注解方式)
- 关于meta
- Java之类的加载器和反射总结
- HDU 1016 Prime Ring Problem (素数环)(DFS)
- CentOS7安装第三方yum源EPEL
- 决策树理论
- 多线程
- CORS 跨域 access-control-allow-headers 的问题
- Windows环境下搭建Solr简易教程