NutzCN Logo
精华 Nutz.dao+druid+Springboot,零文件配置方法。
发布于 3254天前 作者 projack1 7235 次浏览 复制 上一个帖子 下一个帖子
标签:

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的大力指导,总算解决了,共享下。

8 回复

补充springboot默认的application.properties配置

#logging config
logging.level.root=DEBUG

#spring datasource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/cms?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=

org.nutz.dao.impl.FileSqlManager
请问 这个怎么配置 就是 nutz sql 文件的路径

new一个FileSqlManager实例,作为NutDao的构造方法参数

@wendal 您好 能写一篇spring boot 整合nutz的 文章么, 很喜欢Nutz 用了好几年了, 不想放弃啊, 这方便的资料太少了。

@wendal 我是这样写的
@Autowired
public void setDruidDataSource(DruidDataSource druidDataSource,FileSqlManager sqlManager) {
this.druidDataSource = druidDataSource;
this.sqlManager = sqlManager;
setDataSource(druidDataSource);
setSqlManager(sqlManager);
}

@Bean(name="sqlManager")
public FileSqlManager sqlManager(){
String path = "sqls/quer.sql";
FileSqlManager sqlManager = new FileSqlManager(path);
return sqlManager;
}

@wendal OK 了 好用 ,但是 代码感觉有点多, 有时间能 写个整合的文章么?参考下 麻烦了 谢谢

楼主,你好,按你的方法未能成功呢,能否把这部分的源代码共享一下呢,我们也接了一个spring-boot的项目,现在想替了底层,谢谢楼主了

添加回复
请先登陆
回到顶部