The Spring framework is very powerful, but even for a very simple project, we have to configure a lot of things. So there is the Spring Boot framework, its role is very simple, it is to help us automatically configure. The core of the Spring Boot framework is automatic configuration. As long as the corresponding jar package exists, Spring will automatically configure it for us. If the default configuration does not meet the needs, we can also replace the automatic configuration class and use our own configuration. In addition, Spring Boot also integrates many useful features such as embedded Web server, system monitoring, etc., allowing us to quickly build enterprises and applications.
1. Startup class You need to put this class under the root package because it starts scanning from this package down
@SpringBootApplication // Scan for JPA entity classes @EnableJpaRepositories(basePackages = "com.lanwx.entity") @ServletComponentScan // The directory where the mapper is stored @MapperScan("com.lanwx.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
2. Add database and other information under the resources directory
#Database settings 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 class
@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"); //Add XML directory 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. Add mapper class
// Need to add Mapper annotation @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 class
// Note that the import package must be correct @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 resource class
// The resource class only needs the interface and can be used directly @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 class
@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); } }