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();}}
阅读全文
1 0
- SpringBoot学习:SpringBoot整合mybatis
- SpringBoot和Mybatis整合
- springboot 整合mybatis
- springboot+mybatis+dubbo整合
- SpringBoot整合MyBatis
- SpringBoot整合Mybatis
- springBoot整合mybatis
- springboot与mybatis整合
- springboot和mybatis整合
- Springboot整合Mysql+Mybatis
- springboot、mybatis、activemq整合
- SpringBoot整合MyBatis
- SpringBoot 整合mybatis
- SpringBoot + Mybatis 整合demo
- springboot整合mybatis
- SpringBoot-整合MyBatis
- Springboot 整合 Mybatis
- SpringBoot-09整合MyBatis
- JDK学习-Stack/List
- spark机器学习笔记:设计机器学习系统
- JDK学习-Proxy
- 使用box-sizing布局
- JDK学习-IO/NIO
- SpringBoot整合MyBatis
- flandmark人脸检测库的用法
- JDK学习-Map
- 总结一下进程
- State 状态模式 ----对象行为型模式
- 第8章 硬盘和显卡的访问与控制
- 基于ssm框架的个人博客(9)--博客的管理(删除,条件查询)
- Reverse Integer--LeetCode
- 简易解说拉格朗日对偶