终于开始自己写后台了,我现在算java全能了吧,android,java gui和java后端,这是我一个人就能开发一个产品的节奏了
idea:IntelliJ IDEA 2022.1.2 (Community Edition)
数据库:MySQL
依赖:mybatis+mysql+druid
SpringBoot工程创建就不说了
先在pom内引入依赖,版本去https://mvnrepository.com/查
mysql
mysql-connector-java
8.0.29
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.2
com.alibaba
druid
1.2.11
com.alibaba
druid-spring-boot-starter
1.2.11
在application.properties内配置数据库
# 192.168.1.32:3306/test改成自己的数据库,username和password也改成自己的
spring.datasource.url=jdbc:mysql://192.168.1.32:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=xxxx
spring.datasource.password=xxxx
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
新建个包,包名自己随便取,比如我的包名叫mapper,再创建一个interface,加上@Mapper和@Select,写一个简单的查询数据库功能
@Mapper
public interface DBMapper {
@Select("select v.video_name from video v")
List getVideoName();
}
再创建一个类,用来配置数据库和druid,将@MapperScan内的basePackages指向我刚才创建的包,然后关于druid的设置网上很多,而且我还不太会设置,会了再说
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper", sqlSessionFactoryRef = "dbTestSqlSessionFactory")
public class DBTestDataSourceConfig {
@Bean(name="dbTestDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
@Primary
public DruidDataSource dbTestDataSource() {
return new DruidDataSource();
}
@Bean(name="dbTestSqlSessionFactory")
@Primary
public SqlSessionFactory dbTestSqlSessionFactory(@Qualifier("dbTestDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name="dbTestTransactionManager")//配置事务
@Primary
public DataSourceTransactionManager dbTestTransactionManager(@Qualifier("dbTestDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name="dbTestSqlSessionTemplate")
@Primary
public SqlSessionTemplate dbTestSqlSessionTemplate(@Qualifier("dbTestSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name="dbTestDruidServlet")
@Primary
public ServletRegistrationBean dbTestDruidServlet() {
// 进行 druid 监控的配置处理
ServletRegistrationBean srb = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/**");
// 白名单
srb.addInitParameter("allow", "");
// 是否可以重置数据源
srb.addInitParameter("resetEnable", "false");
return srb;
}
@Bean(name="dbTestFilterRegistrationBean")
@Primary
public FilterRegistrationBean dbTestFilterRegistrationBean() {
FilterRegistrationBean frb = new FilterRegistrationBean<>();
frb.setFilter(new WebStatFilter());
// 所有请求进行监控处理
frb.addUrlPatterns("/*");
// 排除名单
frb.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.css,/druid/*");
return frb;
}
}
写个简单的TestController
@RestController
public class TestController {
@Autowired
DBMapper dbMapper;
@GetMapping("test")
@Transactional(transactionManager = "dbTestTransactionManager")
public List test(){
return dbMapper.getVideoName();
}
}
Run一下,然后在网页上输入地址localhost:8080/test/test
把公司数据库内的电视剧给取出来了