以我们经常用的mybatis数据库持久框架来操作mysql服务为例
环境依赖
1.JDK v1.8+ 2.springboot v2.0.3.RELEASE 3.mybatis v3.4.6 4.mysql v10.2.8-MarialDB配置类步骤
1.pom.xml
<!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency>2.springboot配置 application-datasource.properties
#datasource config spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/boot?useSSL=false&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimeZone=GMT%2B8 spring.datasource.username=root spring.datasource.password=123456 #mybatis config mybatis.check-config-location=false mybatis.mapper-locations=classpath*:database/mybatis/mapper/*.xml mybatis.executor-type=reuse代码栗子步骤
1.实体类User.java
package com.example.demo.database.entity; /** * @author nanco * ------------- * demo-springboot * ------------- * @create 2018/10/17 16:52 **/ public class User { private Long id ; private String name ; private Integer age ; private String email ; public Long getId() { return id; } public void setId(Long 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 String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User{" + "id=" + id + ",\'' + ", age=" + age + ", email='" + email + '\'' + '}'; } }2.mapper配置文件 UserMapper.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.demo.database.mysql.dao.UserDao"> <insert parameterType="com.example.demo.database.entity.User"> insert into tbl_user(name,age,email) values(#{name},#{age},#{email}) </insert> </mapper>3.为了使上述配置生效,则须定义扫描入口(@MapperScan)
package com.example.demo.database.config; import com.mysql.jdbc.Driver; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Configuration; /** * @author nanco * ------------- * demo-springboot * ------------- * @create 2018/10/17 17:09 **/ @Configuration public class DatabaseConfig { @Configuration @ConditionalOnClass(Driver.class) @MapperScan("com.example.demo.database.mysql.dao") static class MysqlInterfaceScanner { } }4.来一个测试类
package com.example.demo.database.mysql; import com.example.demo.database.DatabaseApplication; import com.example.demo.database.entity.User; import com.example.demo.database.mysql.dao.UserDao; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.util.Assert; import javax.annotation.Resource; /** * @author nanco * ------------- * demo-springboot * ------------- * @create 2018/10/17 17:13 **/ @RunWith(SpringRunner.class) @SpringBootTest(classes = {DatabaseApplication.class}) public class MysqlDaoTest { @Resource private UserDao userDao; @Test public void testSave() { User user = new User(); user.setName("nanco"); user.setAge(18); user.setEmail("nancoasky@gmail.com"); System.out.println(userDao.saveUser(user)); } }运行上述的测试案例便完成了简单的插入功能,其他的功能读者可自行编写
源码层查阅了spring-boot-autoconfigure包下的spring.factories,发现对于数据源的配置是通过DataSourceAutoConfiguration类来进行的,由此简单的展开下
DataSourceAutoConfiguration本类的注册是有条件的,其类上的注解是
@Configuration @ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class }) @EnableConfigurationProperties(DataSourceProperties.class) @Import({ DataSourcePoolMetadataProvidersConfiguration.class, DataSourceInitializationConfiguration.class })其中DataSource类是JDK自带的,EmbeddedDatabaseType类则是依赖spring-jdbc包,本例中引入mybatis则默认带入了上述包。笔者按照@Configuration的加载顺序来对此类作下简单的分析
1.静态内部类注册解析