夜光序言:
没有人能够感同身受,即使你描述的多么动人。我可以陪你疯,陪你闹,陪你哭,陪你笑,什么都可以,只是你什么都不要。想你的时候有些幸福,幸福得有些难过。
正文:
4.3、springboot整合springjpa
4.3.1 pom文件引入依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> |
4.3.2 创建User实体类
@Entity(name = "users") public class User { @Id @GeneratedValue private Integer id; @Column private String name; @Column private Integer age; // ..get/set方法 } |
4.3.3 创建UserDao
public interface UserDao extends JpaRepository<User, Integer> { } |
4.3.4 创建IndexController
@RestController public class IndexController { @Autowired private UserDao userDao; @RequestMapping("/index") public String index(Integer id) { User findUser = userDao.findOne(id); System.out.println(findUser.getName()); return "success"; } } |
4.3.5 启动项目
@ComponentScan(basePackages = { "com.Genius" }) @EnableJpaRepositories(basePackages = "com.Genius.dao") @EnableAutoConfiguration @ public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } } |
4.4、springboot整合多数据源
同学们思考下,你们在项目中有使用到多数据源吗?
4.4.1配置文件中新增两个数据源
spring.datasource.test1.driverClassName = com.mysql.jdbc.Driver spring.datasource.test1.url = jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf-8 spring.datasource.test1.username = root spring.datasource.test1.password = root
spring.datasource.test2.driverClassName = com.mysql.jdbc.Driver spring.datasource.test2.url = jdbc:mysql://localhost:3306/test02?useUnicode=true&characterEncoding=utf-8 spring.datasource.test2.username = root spring.datasource.test2.password = root |
4.4.2配置文件中新增两个数据源
@Configuration // 注册到springboot容器中 @MapperScan(basePackages = "com.Genius.user1", sqlSessionFactoryRef = "test1SqlSessionFactory") public class DataSource1Config {
/** * * @methodDesc: 功能描述:(配置test1数据库) * @author:夜光 */ @Bean(name = "test1DataSource") @Primary @ConfigurationProperties(prefix = "spring.datasource.test1") public DataSource testDataSource() { return DataSourceBuilder.create().build(); }
/** * * @methodDesc: 功能描述:(test1 sql会话工厂) * @author:夜光 */ @Bean(name = "test1SqlSessionFactory") @Primary public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); // bean.setMapperLocations( // new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml")); return bean.getObject(); }
/** * * @methodDesc: 功能描述:(test1 事物管理) * @author: 夜光 */ @Bean(name = "test1TransactionManager") @Primary public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); }
@Bean(name = "test1SqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate( @Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); }
} |