springboot学习笔记(三)

    xiaoxiao2021-04-15  345

    LK接着上面的,来总结一下springboot与数据源的操作,毕竟做web数据库是少不了的,今天就springboot和mysql整合做一总结: 1. springboot+mybaties整合之注解形式 2. springboot+mybaties整合之xml形式 3. springboot+mybatiesplus+driud数据源整合 4. springboot+mybatiesplus+driud多数据源配置

    1.springboot+mybaties整合之注解形式

    步骤: 添加依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> 配置数据源信息 mybatis.type-aliases-package = com.yrz.model spring.datasource.url = jdbc:mysql:// localhost:3306 / test?serverTimezone = UTC&useUnicode = true&characterEncoding = utf-8&useSSL = true spring.datasource.username = root spring.datasource.password = 123456 spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver mapper接口实例 public interface UserMapper { @Select("SELECT * FROM users") @Results({ @Result(property = "userSex", column = "user_sex"), @Result(property = "nickName", column = "nick_name") }) List<User> getAllUser(); @Select("SELECT * FROM users WHERE id = #{id}") @Results({ @Result(property = "userSex", column = "user_sex"), @Result(property = "nickName", column = "nick_name") }) User getOne(Long id); @Insert("INSERT INTO users(userName,passWord,user_sex) VALUES(#{userName}, #{passWord}, #{userSex})") void insert(User user); @Update("UPDATE users SET userName=#{userName},nick_name=#{nickName} WHERE id =#{id}") void update(User user); @Delete("DELETE FROM users WHERE id =#{id}") void delete(Long id); } Controller用于演示,可以用测试类 @RestController public class UserController { @Autowired private UserMapper userMapper; @RequestMapping("/getUsers") public List<User> getUsers() { List<User> users=userMapper.getAllUser(); return users; } @RequestMapping("/getUser") public User getUser(Long id) { User user=userMapper.getOne(id); return user; } @RequestMapping("/add") public void save(User user) { userMapper.insert(user); } @RequestMapping(value="update") public void update(User user) { userMapper.update(user); } @RequestMapping(value="/delete/{id}") public void delete(@PathVariable("id") Long id) { userMapper.delete(id); } } 启动类上映射mapper @SpringBootApplication @MapperScan("com.yrz.mapper") public class SpringbootMybatiesApplication { public static void main(String[] args) { SpringApplication.run(SpringbootMybatiesApplication.class, args); } } 实例结果 源码 请在 https://github.com/TDYRZ/spring-/tree/master/springboot-mybaties-annotion 上获取

    2.springboot+mybaties整合之xml形式

    LK对XML的配置比较情有独钟,毕竟一直在使用这种形式,springboot也支持xml形式的配置。来看看具体配置: 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.yrz.mapper.UserMapper" > <resultMap id="BaseResultMap" type="com.yrz.model.User" > <id column="id" property="id" jdbcType="BIGINT" /> <result column="userName" property="userName" jdbcType="VARCHAR" /> <result column="passWord" property="passWord" jdbcType="VARCHAR" /> <result column="user_sex" property="userSex" jdbcType="VARCHAR"/> <result column="nick_name" property="nickName" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > id, userName, passWord, user_sex, nick_name </sql> <select id="getAllUser" resultMap="BaseResultMap" > SELECT <include refid="Base_Column_List" /> FROM users </select> <select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" > SELECT <include refid="Base_Column_List" /> FROM users WHERE id = #{id} </select> <insert id="insert" parameterType="com.yrz.model.User" > INSERT INTO users (userName,passWord,user_sex) VALUES (#{userName}, #{passWord}, #{userSex}) </insert> <update id="update" parameterType="com.yrz.model.User" > UPDATE users SET <if test="userName != null">userName = #{userName},</if> <if test="passWord != null">passWord = #{passWord},</if> nick_name = #{nickName} WHERE id = #{id} </update> <delete id="delete" parameterType="java.lang.Long" > DELETE FROM users WHERE id =#{id} </delete> </mapper>

    resource目录下新建一个UserMapper.xml文件夹

    配置文件中配置xml mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.yrz.model spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver dao层实现 @Mapper public interface UserMapper { List<User> getAllUser(); User getOne(Long id); void insert(User user); void update(User user); void delete(Long id); }

    此处使用@Mapper注解,在启动类上就不用在引入dao层包了。 源码请在https://github.com/TDYRZ/spring-/tree/master/springboot-mybites-xml上获取

    3.springboot+mybatiesplus+driud数据源整合

    首先来介绍一下mybites增强工具mybatiesplus,它是在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。 特性: 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求 其他特性请参考官网—https://mp.baomidou.com,废话不多说来看看具体配置依赖引入(mybatisplus-spring-boot-starte和druid) <!-- mybatis-plus begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatisplus-spring-boot-starter</artifactId> <version>${mybatisplus-spring-boot-starter.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>${mybatisplus.version}</version> </dependency> <!-- mybatis-plus end --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> 配置文件(只有单数据源) server: port: 8080 #指定tomcat默认端口号 spring: aop:#aop配置 proxy-target-class: true auto: true datasource: druid: # 数据库 1 url: jdbc:mysql://localhost:3306/db1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver initialSize: 5 minIdle: 5 maxActive: 20 MybatisPlusConfig类用来配置数据源,分页插件,mybaties配置。这里只配置了分页插件 /** * @Author yrz */ @Configuration @MapperScan({"com.yrz.mapper*"}) public class MybatisPlusConfig { /** * mybatis-plus分页插件<br> * 文档:http://mp.baomidou.com<br> */ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); //paginationInterceptor.setLocalPage(true);// 开启 PageHelper 的支持 return paginationInterceptor; } } 配置mapper接口(主要是继承BaseMapper接口) mybaties-plus提供了通用的mapper接口 /** * <p> * Mapper 接口 * </p> * * @author yrz */ public interface UserMapper extends BaseMapper<User> { } 配置service接口(主要是继承IService接口) /** - <p> - 服务类 - </p> - - @author yrz */ public interface UserService extends IService<User> { List<User> getUserList(); } 具体实现类和控制类(主要用于测试) 项目源码 https://github.com/TDYRZ/spring-/tree/master/springboot-mybitesplus-druid

    4. springboot+mybatiesplus+driud多数据源配置

    具体实现思路 使用db1,db2两个数据源。使用AOP在service层进行数据源判断切换。在mybatiesplusConfig类中注入数据源,对其进行全局配置。

    具体代码实现

    配置文件(mysql下两个数据源 db1 db2) server: port: 8080 spring: aop: proxy-target-class: true auto: true datasource: druid: # 数据库 1 db1: url: jdbc:mysql://localhost:3306/db1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver initialSize: 5 minIdle: 5 maxActive: 20 # 数据库 2 db2: url: jdbc:mysql://localhost:3306/db2?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver initialSize: 5 minIdle: 5 maxActive: 20 重点来看看切面类 切点是放在service层,当调用service的方法时切换数据源。 判断方法上是否有数据库切换注解,有的话直接使用注解。 @Component @Aspect public class DataSourceSwitchAspect { @Pointcut("execution(* com.yrz.service.db1.*.*(..))") private void db1Aspect() { } @Pointcut("execution(* com.yrz.service.db2.*.*(..))") private void db2Aspect() { } /** * 执行service方法之前先切换数据源到db1 * @Title: db1 * @Description: TODO(这里用一句话描述这个方法的作用) * @param @param joinPoint 设定文件 * @author:yrz * @data: 2019年5月21日 下午4:48:06 * @return void 返回类型 * @throws */ @Before( "db1Aspect()" ) public void db1(JoinPoint joinPoint) { System.out.println("切换到db1 数据源..."); setDataSource(joinPoint,DBTypeEnum.db1); } /** * 执行service方法之前先切换数据源到db2 * @Title: db1 * @Description: TODO(这里用一句话描述这个方法的作用) * @param @param joinPoint 设定文件 * @author:yrz * @data: 2019年5月21日 下午4:48:06 * @return void 返回类型 * @throws */ @Before("db2Aspect()" ) public void db2 (JoinPoint joinPoint) { System.out.println("切换到db2 数据源..."); setDataSource(joinPoint,DBTypeEnum.db2); } /** * 添加注解方式,如果有注解优先注解,没有则按传过来的数据源配置 * @param joinPoint * @param dbTypeEnum */ private void setDataSource(JoinPoint joinPoint, DBTypeEnum dbTypeEnum) { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); DataSourceSwitch dataSourceSwitch = methodSignature.getMethod().getAnnotation(DataSourceSwitch.class); if (Objects.isNull(dataSourceSwitch) || Objects.isNull(dataSourceSwitch.value())) { DbContextHolder.setDbType(dbTypeEnum); }else{ System.out.println("根据注解来切换数据源,注解值为:"+dataSourceSwitch.value()); switch (dataSourceSwitch.value().getValue()) { case "db1": DbContextHolder.setDbType(DBTypeEnum.db1); break; case "db2": DbContextHolder.setDbType(DBTypeEnum.db2); break; default: DbContextHolder.setDbType(dbTypeEnum); } } } } 不同数据源注入容器 ** * @Author yrz */ @Configuration @MapperScan({"com.yrz.mapper*"}) public class MybatisPlusConfig { /** * mybatis-plus分页插件<br> * 文档:http://mp.baomidou.com<br> */ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); //paginationInterceptor.setLocalPage(true);// 开启 PageHelper 的支持 return paginationInterceptor; } //注入“db1”数据源 @Bean(name = "db1") @ConfigurationProperties(prefix = "spring.datasource.druid.db1" ) public DataSource db1 () { return DruidDataSourceBuilder.create().build(); } //注入“db2”数据源 @Bean(name = "db2") @ConfigurationProperties(prefix = "spring.datasource.druid.db2" ) public DataSource db2 () { return DruidDataSourceBuilder.create().build(); } /** * 动态数据源配置 * @return */ @Bean @Primary public DataSource multipleDataSource (@Qualifier("db1") DataSource db1, @Qualifier("db2") DataSource db2 ) { DynamicDataSource dynamicDataSource = new DynamicDataSource(); Map< Object, Object > targetDataSources = new HashMap<>(); targetDataSources.put(DBTypeEnum.db1.getValue(), db1 ); targetDataSources.put(DBTypeEnum.db2.getValue(), db2); dynamicDataSource.setTargetDataSources(targetDataSources); dynamicDataSource.setDefaultTargetDataSource(db1);//默认数据源 return dynamicDataSource; } //配置mybaties @Bean("sqlSessionFactory") public SqlSessionFactory sqlSessionFactory() throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean(); sqlSessionFactory.setDataSource(multipleDataSource(db1(),db2())); MybatisConfiguration configuration = new MybatisConfiguration(); configuration.setJdbcTypeForNull(JdbcType.NULL); configuration.setMapUnderscoreToCamelCase(true); configuration.setCacheEnabled(false); sqlSessionFactory.setConfiguration(configuration); sqlSessionFactory.setPlugins(new Interceptor[]{ //PerformanceInterceptor(),OptimisticLockerInterceptor() paginationInterceptor() }); sqlSessionFactory.setGlobalConfig(globalConfiguration()); return sqlSessionFactory.getObject(); } //全局配置 @Bean public GlobalConfiguration globalConfiguration() { GlobalConfiguration conf = new GlobalConfiguration(new LogicSqlInjector()); conf.setLogicDeleteValue("-1"); conf.setLogicNotDeleteValue("1"); conf.setIdType(0); // conf.setMetaObjectHandler(new MyMetaObjectHandler()); conf.setDbColumnUnderline(true); conf.setRefresh(true); return conf; } }

    测试结果: 项目源码 https://github.com/TDYRZ/spring-/tree/master/springboot-mybitesplus-druidMultipledatasources

    至此 springboot整合mybaties历经简单到多数据源配置,因为LK一直使用mybaties所以以此作为实例,如果有兴趣可以使用jpa实现。


    最新回复(0)