intellij idea + spring boot + mybatis + druid + maven + mysql + thymeleaf

我的天,csdn编辑器是有毒吗,按了删除键会网页后退,写了很长一篇的东西又白写了


这篇和上一个差不多,只是把data jpa换成了 mybatis ,

csdn编辑器这尿性,懒得写太多说明了,直接贴代码


目录结构

intellij idea + spring boot + mybatis + druid + maven + mysql + thymeleaf_第1张图片


intellij idea + spring boot + mybatis + druid + maven + mysql + thymeleaf_第2张图片






pom.xml   (加了com.github.pagehelper用来分页查询)



	4.0.0

	me.cc
	hgcenter
	0.0.1-SNAPSHOT
	jar

	hgcenter
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.4.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.0
		
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
			org.springframework.boot
			spring-boot-configuration-processor
			true
		

		
			mysql
			mysql-connector-java
		

		
			com.alibaba
			druid
			1.0.28
		

		
			net.sourceforge.nekohtml
			nekohtml
			1.9.22
		

		
			com.github.pagehelper
			pagehelper
			4.1.0
		

	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	






application.properties

#加入这个是为了让thymeleaf语法检查不那么严格,方便写html
spring.thymeleaf.mode = LEGACYHTML5
#开发阶段,模板不要缓存数据,设为false,否则每次改html都要重启项目
spring.thymeleaf.cache = false

spring.jpa.database: MYSQL
# 数据库访问配置
# 主数据源,默认的
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/hgwxm?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=

# 下面为连接池的补充设置,应用到上面所有数据源中
spring.jpa.show-sql = true

# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.datasource.druid.initialSize=5
spring.datasource.druid.minIdle=5
spring.datasource.druid.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.druid.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.druid.minEvictableIdleTimeMillis=300000
spring.datasource.druid.validationQuery=SELECT 1 FROM t_user
spring.datasource.druid.testWhileIdle=true
spring.datasource.druid.testOnBorrow=true
spring.datasource.druid.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.druid.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#spring.datasource.druid.useGlobalDataSourceStat=true



mybatis配置,  参考了  http://blog.csdn.net/catoop/article/details/50553714

package me.cc.Configuration;

import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.pagehelper.PageHelper;

@Configuration
public class MyBatisConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(MyBatisConfiguration.class);

    @Bean
    public PageHelper pageHelper() {
        logger.info("注册MyBatis分页插件PageHelper");
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }

}


实体,实例 Site

package me.cc.enity;

import java.io.Serializable;
import java.util.Date;

public class Site implements Serializable {

    int id;
    String account;
    String name;
    String pass;


mapper,实例  SiteMapper

使用了注解的方式,不用再配置xml,参考  https://github.com/mybatis/mybatis-spring-boot

package me.cc.mapper;

import com.github.pagehelper.Page;
import me.cc.enity.Site;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface SiteMapper {
    @Select("select * from site where id = #{id}")
    Site findById(@Param("id") int id);

    @Select("select * from site")
    Page findAll();


}


使用实例

@Controller
public class HomeController {

    @Autowired
    SiteMapper siteMapper;

    @RequestMapping("/")
    @ResponseBody
    Object home(){
        Site site = siteMapper.findById(1200);

        PageHelper.startPage(1, 3);
        Page page = siteMapper.findAll();

        return page.toPageInfo();
    }
}






你可能感兴趣的:(2.JavaWeb)