SpringBoot整合mybatis各种坑
2019-07-13 19:29
113 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xiegangQN/article/details/95779297
之前ssm框架没有遇到啥问题,但是学习springboot之后各种问题
1.springboot 版本2.1.6.RELEASE
2.mysql 8.0
3.JDK 1.8
4IDEA 2019版
pom文件
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo_mapper</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo_mapper</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <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> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build> </project>
项目架构
pojo对象
public class Student { private Integer id; private String name; private Integer age; public int 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 int getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
Mapper
@Repository public interface StudentMapper { Student getStudents(); }
mapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.example.springbootsql.mapper.StudentMapper" > <select id="getStudents" resultType="com.example.springbootsql.pojo.Student"> select * FROM student where id = 1; </select> </mapper>
service
public interface StudentService { Student getStudent(); }
service实现类
@Service public class StudentServiceImpl implements StudentService { @Autowired public StudentMapper mapper; public Student getStudent(){ return mapper.getStudents(); } }
Controller
@Controller public class StudentController { @Autowired private StudentService studentService; @RequestMapping("/getStu") @ResponseBody public String getStu(){ Student student = studentService.getStudent(); System.out.println(student.getName()); return "sucess"; } }
启动类
@MapperScan("com.example.springbootsql.mapper") @SpringBootApplication public class SpringbootSqlApplication { public static void main(String[] args) { SpringApplication.run(SpringbootSqlApplication.class, args); } }
application.yml
spring: profiles: active: dev
application-dev.yml
server: port: 8080 spring: datasource: username: root password: Changeme_123 url: jdbc:mysql://localhost:3306/taotao?serverTimezone=UTC&useSSL=false driver-class-name: com.mysql.cj.jdbc.Driver mybatis: <!--Mapper.xml--> mapper-locations: classpath:mapping/*.xml <!--映射别名包--> type-aliases-package: com.example.springbootsql.pojo
注意事项
1.因为我用的mysql8.0 所以driver-class-name:com.mysql.cj.jdbc.Driver
2.mapper-locations 后面值classpath:mapping/*.xml(写在mapper文件下不行,不知道为什么)
3.datasource在yml文件中不能用提示的date-username date-password不然会抛一个异常java.sql.SQLException: Access denied for user ‘’@‘localhost’ (using password: NO)
4.启动类一定要加上@MapperScan
5.mapper类用@Repository
相关文章推荐
- spring boot 整合mybatis org.apache.ibatis.binding.BindingException: Invalid bound statement(not found)
- spring boot 整合mybatis
- Spring boot整合mybatis
- SpringBoot整合mybatisplus
- 企业级 SpringBoot 教程 (六)springboot整合mybatis
- Mybatis+Spring boot整合趟坑————自定义TypeHandler
- Spring boot环境搭建(三)- 整合Mybatis
- spring boot整合mybatis使用c3p0数据源连接mysql
- springboot项目与mybatis的整合(使用mybatis小鸟插件)
- Spring Boot整合Mybatis实例
- Springboot整合mybatis
- springboot 整合mybatis
- Spring boot 整合mybatis
- springboot整合mybatis逆向工程
- SpringBoot学习七:springboot整合mybatis详解
- springboot整合mybatis
- Spring Boot整合Mybatis持久层框架
- Spring Boot整合MyBatis,附带单元测试
- Spring Boot、Mybatis框架整合开发Java RESTful Web Service
- SpringBoot整合mybatis