SpringBoot整合MyBatis
2017-10-01 21:01
591 查看
项目结构如图所示
添加pom依赖
<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.chaihuo</groupId>
<artifactId>SpringBootMyBatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--继承 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.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版本 -->
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--热部署.jar -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<!-- <version>1.5.4.RELEASE</version> -->
</dependency>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- MyBatis-SpringBoot -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 热部署的的配置,只有配置了此属性,才能restart -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
修改application.properties
#########################################spring datasource
######################################
spring.datasource.url=jdbc:mysql://localhost:3306/text
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
简单的实体类Student
package com.imooc.entity;public class Student {
private Integer id;
private String name;
private Integer age;
private Integer score;
public Student() {
}
public Student(String name, Integer age, Integer score) {
this.name = name;
this.age = age;
this.score = score;
}
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;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", score=" + score + "]";
}
}
实体类对应的mapper,注意引入的包名
如果你用对应的mappr.xml而不是用的@Insert注解,在properties中加入mybatis.mapper-locations=classpath:xxxxx
######################################
###spring thymeleaf
######################################
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates
######################################
###spring datasource
######################################
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
######################################
###MyBatis
######################################
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
package com.imooc.mapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.imooc.entity.Student;
@Mapper
public interface StudentMapper {
@Insert("insert into student (name,age,score) values(#{name},#{age},#{score})")
public void addStudent(Student stu);
@Delete("delete from student where id =#{id}")
public void deleteById(int id);
@Update("update student set name=#{name},age=#{age},score=#{score} where id=#{id}")
public void update(Student stu);
@Select("select id,name,age,score from student where id = #{id}")
public Student selectById(int id);
}
项目入口类
package com.imooc;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import com.imooc.entity.Student;
import com.imooc.mapper.StudentMapper;
@SpringBootApplication
public class App {
public static void main(String[] args) {
//获得IOC容器
ConfigurableApplicationContext context=SpringApplication.run(App.class, args);
//获得里面的mapper
StudentMapper studentDao=context.getBean(StudentMapper.class);
Student stu=studentDao.selectById(1);
System.out.println(stu);
//关闭项目
context.close();
}
}
相关文章推荐
- Springboot整合dubbo构建maven多模块项目(四) - 集成mybatis
- (2)、Spring Boot与Mybatis的整合( 多数据源)
- SpringBoot入门-快速整合Mybatis
- JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
- spring Boot 整合mybatis 如何使用多数据源
- JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
- springboot+mybatis+dubbo整合
- SpringBoot非官方教程 | 第六篇:springboot整合mybatis
- [置顶] Spring Boot之整合mybatis
- springboot整合Mybatis扫描不到mapper问题
- SpringBoot非官方教程 | 第六篇:springboot整合mybatis
- Spring Boot 整合 Mybatis 实现 Druid 多数据源详解
- Spring Boot整合MyBatis和Druid
- spring boot 整合 mybatis
- SpringBoot整合Freemarker+Mybatis
- SpringBoot(五):SpringBoot整合MyBatis
- Spring-Boot整合mybatis(三)-注解的基本使用
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
- SpringCloud SpringBoot mybatis 分布式微服务(二十一)整合 beatlsql