Springboot集成Mybatis
Posted Weeeeew
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot集成Mybatis相关的知识,希望对你有一定的参考价值。
Springboot 集成mybatis
一、导入Mybatis、JDBC、mysql的starter
二、导入dbcp2或者c3p0的连接池依赖
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> </dependency>
三、在application.properties中设置相关的属性
#Mysql服务器属性配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/myschedule?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=250019
#Mysql连接池
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
spring.datasource.dbcp2.initial-size=10
spring.datasource.dbcp2.max-total=30
spring.datasource.dbcp2.max-wait-millis=1000
spring.datasource.dbcp2.max-idle=10
#mybatis 配置信息
mybatis.mapper-locations=classpath:mapper/*.xml
#转换驼峰与下划线命名法
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.use-generated-keys=true
mybatis.type-aliases-package=com.snapshot2.demo.entiry
mybatis.type-handlers-package=com.snapshot2.demo.typehandler
四、创建mapper接口层,使用@Repository注解声明这是Mapper接口
@Repository public interface LocationDao { /** * 根据UserId 获得Location * @param userId long * @return Location */ public Location getLocationByUserId(long userId); /** * 给一个User 插入一个 Location * @param userId long * @param country String * @param province String * @param city String * @return int 操作成功(1),失败(0) */ public int addLocation(@Param("userId")long userId, @Param("country")String country, @Param("province")String province, @Param("city")String city); }
五、创建entity实体类层,与数据库中的每张表对应
entity 层的创建,映射数据库里的表信息(entity使用驼峰,数据库中使用下划线,保证字段两者相对应)可以使用@Alias来创建别名
package com.snapshot2.demo.entiry; import org.apache.ibatis.type.Alias; @Alias("location") public class Location { private long user_id; private String country; private String province; private String city; public long getUser_id() { return user_id; } public void setUser_id(long user_id) { this.user_id = user_id; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
六、在resource下创建一个mapper文件夹,存放mapper.xml,用于实现mapper接口定义的方法
<?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.snapshot2.demo.dao.LocationDao"> <select id="getLocationByUserId" resultType="Location" parameterType="long"> select user_id, country, province, city from location where user_id = #{userId} </select> <insert id="addLocation" keyProperty="userId" keyColumn="user_id" > insert into location (user_id,country,province,city) values (#{userId},#{country},#{province},#{city}) </insert> </mapper>
七、在程序启动入口使用@MapperSacn注解扫描mapper接口的包
package com.snapshot2.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Repository; //扫描Mapper接口 @MapperScan(basePackages = "com.snapshot2.demo.dao",annotationClass = Repository.class) @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
以上是关于Springboot集成Mybatis的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot集成mybatis以及自动化测试代码实现
SpringBoot 集成MyBatis-Plus提示反序列化异常:cannot deserialize from Object value (no delegate- or property-bas