前言
项目组要搭建一个xxx的后台管理系统
技术选型:
项目:Spring Boot
数据库连接池:Druid
数据库:Oracle或者mysql
JS:VueJS
Html和CSS:Boostrap
一、搭建Spring Boot框架
step1
https://start.spring.io/ 进入此URL,在左边两栏配置自己的GroupID和ArtifactID,在右边栏配置自己需要的一些maven依赖,如web,mysql connector等。点击Generate Project下载配置好的项目zip包。
step2
如果你使用idea或者eclipse(需要安装spring boot插件)也可以快速生成一个项目,idea如下图
start
step3
由于项目比较简单,并发量不大,直接使用界面的mybatis generator 去https://github.com/zouzg/mybatis-generator-gui.git clone下来,maven编译后启动,连接你的数据库就可以使用它来快速生成dao层和mapper文件及model了。
generator
step4
数据库dao层,model,mappers文件都已经生成完了,我们应该连接一下数据库试试了,Druid数据库配置如下:
# 数据库访问配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@ip:port:oratest
spring.datasource.username=xxx
spring.datasource.password=xxx
# 下面为连接池的补充设置,应用到上面所有数据源中
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
spring.datasource.logSlowSql=true
#这里注意增加一个mappers文件的位置,不然会报错
mybatis.mapperLocations=classpath*:mappers/*.xml
光有上面的配置还不行,还需要一个configuration配置,configuration相当于xml文件里面的bean配置,创建的时候直接复制粘贴就行了
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.sql.SQLException;
@Configuration
public class DruidConfig {
private Logger logger = LoggerFactory.getLogger(DruidConfig.class);
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.initialSize}")
private int initialSize;
@Value("${spring.datasource.minIdle}")
private int minIdle;
@Value("${spring.datasource.maxActive}")
private int maxActive;
@Value("${spring.datasource.maxWait}")
private int maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.filters}")
private String filters;
@Value("${spring.datasource.logSlowSql}")
private String logSlowSql;
@Bean
public ServletRegistrationBean druidServlet() {
ServletRegistrationBean reg = new ServletRegistrationBean();
reg.setServlet(new StatViewServlet());
reg.addUrlMappings("/druid/*");
reg.addInitParameter("loginUsername", username);
reg.addInitParameter("loginPassword", password);
reg.addInitParameter("logSlowSql", logSlowSql);
return reg;
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
filterRegistrationBean.addInitParameter("profileEnable", "true");
return filterRegistrationBean;
}
@Bean
public DataSource druidDataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
logger.error("druid configuration initialization filter", e);
}
return datasource;
}
}
不出意外你上面的configuration应该会飘红,因为你没有配置pom文件的依赖。把下面的依赖引入进去,如果你使用的是mysql的connector,就引入mysql的依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--下面是oracle的连接器-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>12.1.0.1-atlassian-hosted</version>
<exclusions>
<exclusion>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--下面是mysql的连接器-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
以为上面配置结束就可以开心的马骝了?NONONO!还有个坑呢,主文件里需要添加@MapperScan,参数为你dao接口的路径,否则就会报bean的相关异常哦!
@MapperScan("com.netease.paygateway.dao")
@SpringBootApplication
public class PaygatewayApplication {
public static void main(String[] args) {
SpringApplication.run(PaygatewayApplication.class, args);
}
}
step4
写一个Junit测试类测一测,在main目录的兄弟目录test下创建一个测试类,也可以在某个dao类直接使用ctrl+shift+T(idea),在类上右键新建junit(eclipse)。注意添加两个注解,@SpringBootTest里面的注解就是你的主类Application。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PaygatewayApplication.class)
public class ChargeServiceTest {
@Autowired
ChargeDAO chargeDAO;
@Test
public void getChargeVOList() {
ChargeVOExample example = new ChargeVOExample();
example.createCriteria().andChargeIdEqualTo("2018052415GCR30503164");
List<ChargeVO> result = chargeDAO.selectByExample(example);
System.out.println(result);
}
}
网友评论