SpringBoot整合MyBatis

来源:互联网 发布:java生成六位验证码 编辑:程序博客网 时间:2024/06/03 16:43

项目结构如图所示


添加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/textspring.datasource.username=rootspring.datasource.password=rootspring.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;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", age=" + age + ", score=" + score + "]";}}


实体类对应的mapper,注意引入的包名

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;@Mapperpublic 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;@SpringBootApplicationpublic class App {public static void main(String[] args) {//获得IOC容器ConfigurableApplicationContext context=SpringApplication.run(App.class, args);//获得里面的mapperStudentMapper studentDao=context.getBean(StudentMapper.class);Student stu=studentDao.selectById(1);System.out.println(stu);//关闭项目context.close();}}



原创粉丝点击