Spring框架功能很强大,但是就算是一个很简单的项目,我们也要配置很多东西。 因此就有了Spring Boot框架,它的作用很简单,就是帮我们自动配置。Spring Boot框架的核心就是自动配置,只要存在相应的jar包,Spring就帮我们自动配置。 如果默认配置不能满足需求,我们还可以替换掉自动配置类,使用我们自己的配置。另外,Spring Boot还集成了嵌入式的Web服务器,系统监控等很多有用的功,让我们快速构建企业及应用程序。
1.启动类 需要把该类放到根包下面 因为都是从这个包开始往下开始扫描
@SpringBootApplication // 扫描JPA实体类 @EnableJpaRepositories(basePackages = "com.lanwx.entity") @ServletComponentScan // 存放mapper的目录 @MapperScan("com.lanwx.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
2.在resources目录下面添加数据库等信息
#数据库设置 spring.jpa.database=oracle spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver spring.datasource.url=jdbc:oracle:thin:@xx:xx spring.datasource.username=xx spring.datasource.password=xx
3.mybatis配置类
@Configuration @ConditionalOnClass({ EnableTransactionManagement.class, EntityManager.class }) @MapperScan(basePackages={"com.xxx.mapper"}) public class MyBatisConfig implements EnvironmentAware{ private RelaxedPropertyResolver propertyResolver; @Override public void setEnvironment(Environment env) { this.propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource."); } @Bean(name="dataSource", destroyMethod = "close", initMethod="init") @Primary public DataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(propertyResolver.getProperty("url")); datasource.setDriverClassName(propertyResolver.getProperty("driverClassName")); datasource.setUsername(propertyResolver.getProperty("username")); datasource.setPassword(propertyResolver.getProperty("password")); datasource.setValidationQuery("select count(1) from dual"); return datasource; } @Bean(name="sqlSessionFactory") @ConditionalOnMissingBean public SqlSessionFactory sqlSessionFactory() { try { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); sessionFactory.setTypeAliasesPackage("com.xxx.entity"); //添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sessionFactory.setMapperLocations(resolver.getResources("classpath:com/xxx/**/mapper/*.xml")); return sessionFactory.getObject(); } catch (Exception e) { return null; } } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } // @Bean // public RoundRobinRWRoutingDataSourceProxy roundRobinDataSouceProxy(){ // RoundRobinRWRoutingDataSourceProxy proxy = new RoundRobinRWRoutingDataSourceProxy(); // proxy.setWriteDataSource(writeDataSource); // proxy.setReadDataSoures(readDataSources); // proxy.setReadKey("READ"); // proxy.setWriteKey("WRITE"); // // return proxy; // } @Bean @ConditionalOnMissingBean public DataSourceTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } }
4.添加mapper类
// 需要加Mapper注解 @Mapper public interface xxxxxMapper{ @Select("select count(1) from xx ") int xxxxxxx(); @Select("select * from xx where bui_id=#{xxxx}") xxx xxxxxxxx(@Param("xxx") String xxx); }
5.JPA实体类
// 注意引入包要正确 @Entity @Table(name="xxx") public class xxx implements Serializable{ private static final long serialVersionUID = 2425973233219442129L; @Id private String id ; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } @Column(name = "NAME") public String getName() { return name; } public void setName(String name) { this.name = name; } }
6.JPA资源类
// 资源类只需要接口就能直接使用 @Repository @Table(name = "xxx") @Qualifier("xxxxx") public interface xxx extends JpaRepository<xxxx, String>{ public xxxx findOne(String id); @SuppressWarnings("all") public xxx save(Lanwx01 u); @Query("select t from xxx t where t.id=:id") public xx findUserByName(@Param("id") String id); }
7.Controller类
@RestController @EnableAutoConfiguration public class TestController { private static final Logger logger = LoggerFactory.getLogger(TestController.class); @Resource private MyService myService ; @Resource private xxxMapper xxxx; @Resource private xxxRepository xxxRepository; @RequestMapping("/xxx") public xxxx findzCC(@RequestParam String buiId){ System.out.println(xxxx.xxxx(buiId)); return xxx.findLanwx(buiId); } }