Spring Boot中整合MyBatis,相关问题总结
2019-01-05 22:42
661 查看
Spring Boot 整合MyBatis步骤:
- 这里用到spring-boot-starter基础和spring-boot-starter-test用来做单元测试验证数据访问
- 引入连接mysql的必要依赖mysql-connector-java
- 引入整合MyBatis的核心依赖mybatis-spring-boot-starter
- 这里不引入spring-boot-starter-jdbc依赖,是由于mybatis-spring-boot-starter中已经包含了此依赖
[code] <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.3.2.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>2.1.1.RELEASE</version> </dependency> </dependencies>
- 同之前介绍的使用jdbc和spring-data连接数据库一样,在application.properties中配置mysql的连接配置
[code]spring.datasource.url=jdbc:mysql://localhost:3306/testmybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
相关问题:
1、mysql 和 JDBC连接出现时区上的问题:java.sql.SQLException: The server time zone value '???ú±ê×??±??' is unrecognized or
解决方案:jdbc后面加上serverTimezone=UTC或者GMT
[code]spring.datasource.url=jdbc:mysql://localhost:3306/testmybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 //或者 spring.datasource.primary.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
2、较新版的Spring Boot取消了@SpringApplicationConfiguration这个注解,用@SpringBootTest就可以了。
3、@Mapper ///Mapper的作用:自动生成增删改查的SQL语句 大大减化对单表的操作
4、其他相关问题参考如下代码:
[code]@Mapper ///Mapper的作用:自动生成增删改查的SQL语句 大大减化对单表的操作 public interface UserMapper { @Delete("DELETE FROM User") // WHERE name=#{name} void delete();//注意返回类型 @Update("UPDATE User SET age=#{age} WHERE name=#{name}") void update(User user); @Select("SELECT * FROM User WHERE name = #{name}") User findByName(@Param("name") String name); @Insert("INSERT INTO User(name,age) VALUES(#{name}, #{age})") int insert(@Param("name") String name,@Param("age") Integer age); //通过Map进行插入数据记录 @Insert("INSERT INTO User(name,age) VALUES(#{name,jdbcType=VARCHAR},#{age,jdbcType=INTEGER})") int insertByMap(Map<String,Object> map); //通过使用普通的java对象进行插入数据记录 @Insert("INSERT INTO User(id,name,age) VALUES(#{id}, #{name}, #{age})") int insertByUser(User user); //对于数据库的查询操作,往往需要进行多表关联,汇总计算等操作, // 所以往往需要返回一个与数据库实体不同的包装类,使用@Results和@Result注解进行绑定 @Results({ @Result(property = "name",column = "name"), @Result(property = "age",column = "age") }) @Select("SELECT name, age FROM user") List<User> findAll(); //在上面代码中,@Result中的property属性对应User对象中的成员名,column对应SELECT出的字段名。 // 在该配置中故意没有查出id属性,只对User对应中的name和age对象做了映射配置, // 这样可以通过下面的单元测试来验证查出的id为null,而其他属性不为null }
针对使用
@Results和
@Result注解来进行结果绑定的测试代码如下:
[code]@Test @Rollback public void testUserMapper() throws Exception{ List<User> userList = userMapper.findAll(); for (User user : userList){ Assert.assertEquals(null,user.getId()); System.out.println(user.getName()+" "+user.getAge()); } }
相关文章推荐
- Maven项目中,关于Spring Boot 整合MyBatis时,Service层无法找到mapper接口的问题解决
- springboot整合mybatis和pageHelper异常问题记录
- Spring Boot相关知识(三) Spring Boot项目整合MyBatis
- 自己用springboot+mybatis+easyui写的个人管理系统遇到的问题总结
- SpringBoot整合mybatis出现时区问题
- Springmvc+mybatis+Spring整合遇到的问题总结
- SpringMvc+MyBatis+Spring+Maven整合遇到的问题总结
- spring boot + maven + mybatis 学习过程遇到问题总结(1)
- 关于IDEA工具在springboot整合mybatis中出现的Invalid bound statement (not found)问题
- Springboot整合Mybatis的CallSettersOnNulls配置问题
- [小技巧]关于spring boot 整合mybatis-plus: value为null时不返回的问题。
- IDEA创建一个spring-boot整合Mybatis的模块时,遇到的问题
- springboot+mybatis+thymeleaf关于前后台日期格式化问题总结
- 关于spring boot整合mybatis使用oracle数据库出现could not load:oracle.jdbc.driver.OracleDriver问题的终极解决方案
- springboot与mybatis整合dao层不能注入的问题
- spring boot mybatis相关问题
- springboot整合Mybatis扫描不到mapper问题
- springboot多模块化整合mybatis,出现mapper自动注入失败问题
- springboot整合mybatis中的问题及出现的一些问题小结
- springboot整合com.baomidou.mybatisplus出现ID参数类型不匹配的问题