1、依赖pom.xml
<dependencies> <!-- web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--启动时启动内置tomcat --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!--对Jsp支持 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- 支持jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.46</version> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- MySql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.22</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.6</version> </dependency> </dependencies>
2、配置数据库连接信息application.properties
#datasource:user spring.datasource.user.url=jdbc:mysql://localhost:3306/licy?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true spring.datasource.user.username=root spring.datasource.user.password=root spring.datasource.user.driver=com.mysql.jdbc.Driver #datasource:task spring.datasource.task.url=jdbc:mysql://localhost:3306/qding_hk_user?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true spring.datasource.task.username=root spring.datasource.task.password=root spring.datasource.task.driver=com.mysql.jdbc.Driver
3、创建实体src/main/java/pom/domain/Task.java、src/main/java/pom/domain/User.java
@Data @NoArgsConstructor @AllArgsConstructor public class Task { private int id; private String name; private String cron; private String className; private String methodName; private String isDeleted; }
@Data @NoArgsConstructor @AllArgsConstructor public class User { private int id; private String name; }
4、创建dao类src/main/java/pom/dao/task/TaskDao.java、src/main/java/pom/dao/user/UserDao.java
public interface TaskDao { Task getTaskById(Task task); }
public interface UserDao { User getUserById(User user); }
5、创建数据库文件src/main/resources/mapper/task/TaskMapper.xml、resources/mapper/user/UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="pom.dao.task.TaskDao"> <select id="getTaskById" resultType="pom.domain.Task"> SELECT * FROM task WHERE deleted = 0 and ID = #{id} </select> </mapper>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="pom.dao.user.UserDao"> <select id="getUserById" parameterType="pom.domain.User" resultType="pom.domain.User"> SELECT * FROM user WHERE ID = #{id} </select> </mapper>
6、创建配置文件src/main/java/pom/config/TaskMapperConfig.java、src/main/java/pom/config/UserMapperConfig.java
@Configuration @MapperScan(basePackages = { "pom.dao.task" }, sqlSessionFactoryRef = "taskSqlSessionFactory") public class TaskMapperConfig { @Value("${spring.datasource.task.url}") private String url; @Value("${spring.datasource.task.username}") private String user; @Value("${spring.datasource.task.password}") private String password; @Value("${spring.datasource.task.driver}") private String driverClass; @Bean(name = "taskDataSource") @Primary public DataSource masterDataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); return dataSource; } @Bean(name = "taskTransactionManager") @Primary public DataSourceTransactionManager masterTransactionManager() { return new DataSourceTransactionManager(masterDataSource()); } @Bean(name = "taskSqlSessionFactory") @Primary public SqlSessionFactory masterSqlSessionFactory(@Qualifier("taskDataSource") DataSource masterDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(masterDataSource); sessionFactory.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath:mapper/task/TaskMapper.xml")); return sessionFactory.getObject(); } }
@Configuration @MapperScan(basePackages = { "pom.dao.user" }, sqlSessionFactoryRef = "userSqlSessionFactory") public class UserMapperConfig { @Value("${spring.datasource.user.url}") private String url; @Value("${spring.datasource.user.username}") private String user; @Value("${spring.datasource.user.password}") private String password; @Value("${spring.datasource.user.driver}") private String driverClass; @Bean(name = "userDataSource") public DataSource master2DataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); return dataSource; } @Bean(name = "userTransactionManager") public DataSourceTransactionManager master2TransactionManager() { return new DataSourceTransactionManager(master2DataSource()); } @Bean(name = "userSqlSessionFactory") public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("userDataSource") DataSource master2DataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(master2DataSource); sessionFactory.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath:mapper/user/UserMapper.xml")); return sessionFactory.getObject(); } }