[size=large][color=red][b]初识spring boot[/b][/color][/size]
Spring框架功能很强大,但是就算是一个很简单的项目,我们也要配置很多东西。[color=red][b]因此就有了Spring Boot框架,它的作用很简单,就是帮我们自动配置。[/b][/color]Spring Boot框架的核心就是自动配置,只要存在相应的jar包,Spring就帮我们自动配置。[b]如果默认配置不能满足需求,我们还可以替换掉自动配置类,使用我们自己的配置。[/b]另外,Spring Boot还集成了嵌入式的Web服务器,系统监控等很多有用的功,让我们快速构建企业及应用程序。
[size=medium][color=red][b]1.启动类 需要把该类放到根包下面 因为都是从这个包开始往下开始扫描
[/b][/color][/size]
[size=medium][color=red][b]2.在resources目录下面添加数据库等信息[/b][/color][/size]
[size=medium][color=red][b]3.mybatis配置类[/b][/color][/size]
[size=medium][color=red][b]4.添加mapper类 [/b][/color][/size]
[size=medium][color=red][b]5.JPA实体类[/b][/color][/size]
[size=medium][color=red][b]6.JPA资源类[/b][/color][/size]
[size=medium][color=red][b]7.Controller类[/b][/color][/size]
Spring框架功能很强大,但是就算是一个很简单的项目,我们也要配置很多东西。[color=red][b]因此就有了Spring Boot框架,它的作用很简单,就是帮我们自动配置。[/b][/color]Spring Boot框架的核心就是自动配置,只要存在相应的jar包,Spring就帮我们自动配置。[b]如果默认配置不能满足需求,我们还可以替换掉自动配置类,使用我们自己的配置。[/b]另外,Spring Boot还集成了嵌入式的Web服务器,系统监控等很多有用的功,让我们快速构建企业及应用程序。
[size=medium][color=red][b]1.启动类 需要把该类放到根包下面 因为都是从这个包开始往下开始扫描
[/b][/color][/size]
@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);
}
}
[size=medium][color=red][b]2.在resources目录下面添加数据库等信息[/b][/color][/size]
#数据库设置
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
[size=medium][color=red][b]3.mybatis配置类[/b][/color][/size]
@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());
}
}
[size=medium][color=red][b]4.添加mapper类 [/b][/color][/size]
// 需要加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);
}
[size=medium][color=red][b]5.JPA实体类[/b][/color][/size]
// 注意引入包要正确
@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;
}
}
[size=medium][color=red][b]6.JPA资源类[/b][/color][/size]
// 资源类只需要接口就能直接使用
@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);
}
[size=medium][color=red][b]7.Controller类[/b][/color][/size]
@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);
}
}