1、POM依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.16</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz</artifactId>
<version>1.r.54</version>
</dependency>
2、DruidConfiguration.class(druid的配置,用springboot推荐的java config方式)
@Configuration
public class DruidConfiguration {
@Bean
public ServletRegistrationBean druidServlet() {
return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
}
@Bean(name="druidDataSource")
public DataSource druidDataSource(@Value("${spring.datasource.driverClassName}") String driver,
@Value("${spring.datasource.url}") String url,
@Value("${spring.datasource.username}") String username,
@Value("${spring.datasource.password}") String password) {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driver);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
try {
druidDataSource.setFilters("stat, wall");
} catch (SQLException e) {
e.printStackTrace();
}
return druidDataSource;
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}
3、CmsDao(自定义项目的dao)
@Configuration
@Component("dao")
public class CmsDao extends NutDao implements Dao{
DataSource druidDataSource;
@Autowired //这里注解只能写到set方法上,不然druidDataSource获取不到,也是坑了我很久。请留意。
public void setDruidDataSource(DataSource druidDataSource) {
this.druidDataSource = druidDataSource;
setDataSource(druidDataSource);
}
public void run(ConnCallback callback) {
Connection con = DataSourceUtils.getConnection(druidDataSource);
try {
callback.invoke(con);
} catch (Exception e) {
if (e instanceof RuntimeException)
throw (RuntimeException) e;
else
throw new RuntimeException(e);
} finally {
DataSourceUtils.releaseConnection(con, druidDataSource);
}
}
}
4、XcmsApplication.class (springboot启动文件)
@SpringBootApplication
//@ImportResource(value="classpath:application-nutz.xml")
public class XcmsApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication();
app.setBannerMode(Banner.Mode.OFF);
app.run(XcmsApplication.class, args);
}
}
5、XcmsApplicationTests.class(springboot测试用例)
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=XcmsApplication.class)
@WebAppConfiguration
public class XcmsApplicationTests {
@Autowired
Dao dao;
@Test
public void testdao() {
System.out.println("test dao query size is :" + dao.query("tablname",null).size());
}
}
没办法,中Nutz.dao毒太深,接手啥系统,第一时间想到的都是替换dao层,经wendal的大力指导,总算解决了,共享下。