您的位置:首页 > 编程语言 > Java开发

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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息