MyException - 我的异常网
当前位置:我的异常网» 行业应用 » 高速搭建一个简单的spring-boot工程(ssm)

高速搭建一个简单的spring-boot工程(ssm)

www.MyException.Cn  网友分享于:2013-03-06  浏览:0次
快速搭建一个简单的spring-boot工程(ssm)

本示例工程整合spring-web、spring-jdbc、mybatis、druid、pageHelper等主流框架,附件为整个工程代码,下载后可以直接运行。

 

1、通过 SPRING INITIALIZR新建一个web工程,可先将Web,Aspects,JDBC,MyBatis,MySQL等依赖引入;

 

2、将生成的web工程导入IDE,本人用的STS;

 

3、本示例会将druid、pageHelper集成到项目中,故先添加相关依赖;

 

	<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.26</version>
        </dependency>
        
        <dependency>
		    <groupId>com.github.pagehelper</groupId>
		    <artifactId>pagehelper</artifactId>
		    <version>4.1.6</version>
		</dependency>

 

 

4、SPRING INITIALIZR默认生成的配置文件名称为application.properties,本示例使用application.yml(可以直接修改后缀),由于引入了数据库连接驱动,这里直接启动会报异常,我们先配置application.yml文件;

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/bds?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true  
    logSlowSql: true
    filters: stat,wall,log4j
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    useGlobalDataSourceStat: true
  druid: 
    loginUsername: lihua
    loginPassword: lihua
    
  mvc:
    view:
      prefix: /WEB-INF/
      suffix: .jsp
    
server:
  port: 8083
  tomcat:
    uri-encoding: UTF-8
  
mybatis:
  mapper-locations:
  - classpath*:mapper/mysql/*.xml
  type-aliases-package: com.huatech.domain
    

 5、创建数据库表(为演示方便,本示例创建user表)

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `create_time` datetime DEFAULT NULL,
  `username` varchar(32) DEFAULT NULL,
  `email` varchar(32) DEFAULT NULL,
  `remark` varchar(128) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;

 

6、新建user 实体类,简单实现三层(DAO/Service/Controller)代码

package com.huatech.domain;

import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;

public class User {
	
	private Long id;
	
	// 日期类型输出到页面格式  
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")    
    // 页面字符串格式化为日期  
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")  
	private Date createTime;
	private String username;
	private String email;
	private String remark;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	@Override
	public String toString() {
		return "User [id=" + id + ", createTime=" + createTime + ", username=" + username + ", email=" + email
				+ ", remark=" + remark + "]";
	}
	
}

 

package com.huatech.mapper;

import java.util.List;

import com.huatech.domain.User;

/**
 * Dao Interface:UserMapper
 * @author lh
 * @version 3.0
 * @date 2017-12-26
 */
public interface UserMapper {
	
	void insert(User user);	
	
	User get(Long id);
	
	List<User> findList(User model);

}

 

package com.huatech.service;

import java.util.List;

import com.huatech.domain.User;

public interface UserService {
	
	void insert(User user);	
	
	User get(Long id);
	
	List<User> findList(User model);

}

 

package com.huatech.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huatech.domain.User;
import com.huatech.mapper.UserMapper;
import com.huatech.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService{

	@Autowired
	private UserMapper dao;
	
	@Override
	public void insert(User user) {
		dao.insert(user);
	}

	@Override
	public User get(Long id) {
		return dao.get(id);
	}
	
	@Override
	public List<User> findList(User model) {
		return dao.findList(model);
	}
	
}

 

package com.huatech.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huatech.domain.User;
import com.huatech.service.UserService;

@Controller
public class UserController {
	
	@Autowired private UserService userService;

	@GetMapping(value="/user/addPage")
	public String addPage(){
		return "user/addPage";
	}
	
	@PostMapping(value="/user/doAdd")
	@ResponseBody
	public User doAdd(User user){
		userService.insert(user);
		return user;
	}
	
	
	
	
}

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.huatech.mapper.UserMapper">
	<resultMap type="com.huatech.domain.User" id="userResultMap">
		<result property="id" column="ID"/>
		<result property="createTime" column="CREATE_TIME"/>
		<result property="username" column="USERNAME"/>
		<result property="email" column="EMAIL"/>
		<result property="remark" column="REMARK"/>
	</resultMap>
	
	<sql id="table_columns">
		ID,
		CREATE_TIME,
		USERNAME,
		EMAIL,
		REMARK
    </sql>
	<sql id="entity_properties">
		#{id},
		#{createTime},
		#{username},
		#{email},
		#{remark}
	</sql>

    <!-- 使用like用法:columnName like concat('%',#{columnName},'%') -->
    <sql id="page_where">
        <trim prefix="where" prefixOverrides="and | or ">
			<if test="id != null">and ID = #{id}</if>
			<if test="createTime != null">and CREATE_TIME = #{createTime}</if>
            <if test="username != null and username != ''">and USERNAME = #{username}</if>
            <if test="email != null and email != ''">and EMAIL = #{email}</if>
            <if test="remark != null and remark != ''">and REMARK = #{remark}</if>
        </trim>
        <if test="page != null and page.sort!=null and page.sort!=''">
        	order by ${page.sort} ${page.order}
        </if>
        <!-- <if test="page == null or page.sort == null or page.sort == ''">order by sort </if> --> 
    </sql>

	<insert id="insert" parameterType="com.huatech.domain.User" useGeneratedKeys="true" keyProperty="id">
		insert into user( CREATE_TIME,
		USERNAME,
		EMAIL,
		REMARK ) 
		values ( #{createTime},
		#{username},
		#{email},
		#{remark} )
	</insert>
	
	<insert id="insertBatch" parameterType="java.util.List">		
		insert into user( CREATE_TIME,
		USERNAME,
		EMAIL,
		REMARK ) 
		values 
		<foreach collection="list" item="item" index="index" separator=",">  
		( 
				#{item.createTime},
				#{item.username},
				#{item.email},
				#{item.remark}
		)
		</foreach>
	</insert>

	<update id="update" parameterType="com.huatech.domain.User">
		update user 
		<trim prefix="set" suffixOverrides=",">
			<if test="createTime != null">CREATE_TIME = #{createTime},</if>
            <if test="username != null and username != ''">USERNAME = #{username},</if>
            <if test="email != null and email != ''">EMAIL = #{email},</if>
            <if test="remark != null and remark != ''">REMARK = #{remark},</if>
		</trim>
		<where>id = #{id}</where>
	</update>

    <select id="findAll" resultMap="userResultMap">
        select <include refid="table_columns" />
        from user
    </select>

    <select id="findList" resultMap="userResultMap">
        select <include refid="table_columns" />
        from user
        <include refid="page_where" />
	</select>

    <select id="getCount" resultType="int" >
        select count(id) from user
        <include refid="page_where" />
    </select>


    <select id="get" resultMap="userResultMap" parameterType="String" >
		select <include refid="table_columns" />
		from user
		where id = #{id}
	</select>
	
	<!-- 其他自定义SQL -->


</mapper>

 7、MVC层配置,包括首页视图配置、设置字符编码过滤器、设置转换器等等;

package com.huatech.supports;

import java.nio.charset.Charset;
import java.util.List;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class ServletConfig extends WebMvcConfigurerAdapter {
	
	private static final String CHARSET = "UTF-8";

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		Charset charset = Charset.forName(CHARSET);
		StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(charset);
		MappingJackson2HttpMessageConverter jsonConverter = new  MappingJackson2HttpMessageConverter();
		converters.add(stringConverter);	
		converters.add(jsonConverter);			
		super.configureMessageConverters(converters);  
	}
	
	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
	
	@Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }
	
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("index");
	}
	
	@Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setForceEncoding(true);
        characterEncodingFilter.setEncoding(CHARSET);
        registrationBean.setFilter(characterEncodingFilter);
        return registrationBean;
    }

}

 8、druid配置

package com.huatech.supports;

import java.sql.SQLException;
import javax.sql.DataSource;
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 org.springframework.context.annotation.Primary;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

@Configuration
public class DruidConfig {

	@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.poolPreparedStatements}")
	private boolean poolPreparedStatements;

	@Value("${spring.datasource.filters}")
	private String filters;

	@Value("${spring.datasource.logSlowSql}")
	private String logSlowSql;
	
	@Value("${spring.druid.loginUsername}")
	private String loginUsername;
	@Value("${spring.druid.loginPassword}")
	private String loginPassword;

	@Bean
	@Primary
	public DataSource dataSource() {
		// @Primary 注解作用是当程序选择dataSource时选择被注解的这个
		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);
		datasource.setPoolPreparedStatements(poolPreparedStatements);
		try {
			datasource.setFilters(filters);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return datasource;
	}

	@Bean
	public ServletRegistrationBean druidServlet() {
		ServletRegistrationBean reg = new ServletRegistrationBean();
		reg.setServlet(new StatViewServlet());
		reg.addUrlMappings("/druid/*");
		reg.addInitParameter("loginUsername", loginUsername);
		reg.addInitParameter("loginPassword", loginPassword);
		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;
	}
}

 9、PageHelper配置

package com.huatech.supports;

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.pagehelper.PageHelper;

@Configuration
public class PageHelperConfig {
	
	/**
	 * 配置mybatis的分页插件pageHelper
	 * @return
	 */
    @Bean
    public PageHelper pageHelper(){
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum","true");
        properties.setProperty("rowBoundsWithCount","true");
        properties.setProperty("reasonable","true");
        //properties.setProperty("autoRuntimeDialect","true");
        //properties.setProperty("dialect","mysql");    //配置mysql数据库的方言
        pageHelper.setProperties(properties);
        return pageHelper;
    }

}

 10、开启事务并配置Mapper扫包路径

package com.huatech;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableTransactionManagement
@MapperScan("com.huatech.mapper")
public class SpringBootSsmDemoApplication extends SpringBootServletInitializer {
	
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(SpringBootSsmDemoApplication.class);
	}

	public static void main(String[] args) {
		SpringApplication.run(SpringBootSsmDemoApplication.class, args);
	}
}

 

整合过程中遇到的问题:

1、web工程可以正常启动,但是不能访问。请检查你的pom文件是否引入tomcat插件依赖。

		<!-- Provided -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>

 2、中文乱码问题

      如果是数据库层面乱码,请检查一下数据库和表的编码格式是否为utf-8,另外检查一下application.yml中spring.datasource.url中是否设置编码格式?useUnicode=true&characterEncoding=UTF-8。

      如果是程序中出现乱码,比如页面输出乱码,请检查CharacterEncodingFilter设置的编码方式和MessageConverter的编码方式,可以参考ServletConfig.java类。具体配置如下:

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		Charset charset = Charset.forName(CHARSET);
		StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(charset);
		MappingJackson2HttpMessageConverter jsonConverter = new  MappingJackson2HttpMessageConverter();
		converters.add(stringConverter);	
		converters.add(jsonConverter);			
		super.configureMessageConverters(converters);  
	}

 

	@Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setForceEncoding(true);
        characterEncodingFilter.setEncoding(CHARSET);
        registrationBean.setFilter(characterEncodingFilter);
        return registrationBean;
    }

       另一种情况可能是web server编码格式未正确设置所致,在application.yml中配置如下:

server:
  port: 8083
  tomcat:
    uri-encoding: UTF-8

 3、Druid配置未起作用,请检查application.yml中spring.datasource.type是否正确设置。

文章评论

我是如何打败拖延症的
我是如何打败拖延症的
看13位CEO、创始人和高管如何提高工作效率
看13位CEO、创始人和高管如何提高工作效率
 程序员的样子
程序员的样子
10个帮程序员减压放松的网站
10个帮程序员减压放松的网站
十大编程算法助程序员走上高手之路
十大编程算法助程序员走上高手之路
10个调试和排错的小建议
10个调试和排错的小建议
一个程序员的时间管理
一个程序员的时间管理
2013年美国开发者薪资调查报告
2013年美国开发者薪资调查报告
编程语言是女人
编程语言是女人
漫画:程序员的工作
漫画:程序员的工作
当下全球最炙手可热的八位少年创业者
当下全球最炙手可热的八位少年创业者
程序员的一天:一寸光阴一寸金
程序员的一天:一寸光阴一寸金
Web开发人员为什么越来越懒了?
Web开发人员为什么越来越懒了?
程序员最害怕的5件事 你中招了吗?
程序员最害怕的5件事 你中招了吗?
程序员应该关注的一些事儿
程序员应该关注的一些事儿
如何区分一个程序员是“老手“还是“新手“?
如何区分一个程序员是“老手“还是“新手“?
老美怎么看待阿里赴美上市
老美怎么看待阿里赴美上市
Java程序员必看电影
Java程序员必看电影
2013年中国软件开发者薪资调查报告
2013年中国软件开发者薪资调查报告
鲜为人知的编程真相
鲜为人知的编程真相
程序员周末都喜欢做什么?
程序员周末都喜欢做什么?
那些争议最大的编程观点
那些争议最大的编程观点
做程序猿的老婆应该注意的一些事情
做程序猿的老婆应该注意的一些事情
“肮脏的”IT工作排行榜
“肮脏的”IT工作排行榜
我的丈夫是个程序员
我的丈夫是个程序员
程序员必看的十大电影
程序员必看的十大电影
程序员和编码员之间的区别
程序员和编码员之间的区别
Web开发者需具备的8个好习惯
Web开发者需具备的8个好习惯
那些性感的让人尖叫的程序员
那些性感的让人尖叫的程序员
Java 与 .NET 的平台发展之争
Java 与 .NET 的平台发展之争
“懒”出效率是程序员的美德
“懒”出效率是程序员的美德
要嫁就嫁程序猿—钱多话少死的早
要嫁就嫁程序猿—钱多话少死的早
总结2014中国互联网十大段子
总结2014中国互联网十大段子
Google伦敦新总部 犹如星级庄园
Google伦敦新总部 犹如星级庄园
5款最佳正则表达式编辑调试器
5款最佳正则表达式编辑调试器
亲爱的项目经理,我恨你
亲爱的项目经理,我恨你
程序员眼里IE浏览器是什么样的
程序员眼里IE浏览器是什么样的
老程序员的下场
老程序员的下场
程序猿的崛起——Growth Hacker
程序猿的崛起——Growth Hacker
中美印日四国程序员比较
中美印日四国程序员比较
我跳槽是因为他们的显示器更大
我跳槽是因为他们的显示器更大
团队中“技术大拿”并非越多越好
团队中“技术大拿”并非越多越好
不懂技术不要对懂技术的人说这很容易实现
不懂技术不要对懂技术的人说这很容易实现
每天工作4小时的程序员
每天工作4小时的程序员
代码女神横空出世
代码女神横空出世
程序员都该阅读的书
程序员都该阅读的书
为什么程序员都是夜猫子
为什么程序员都是夜猫子
什么才是优秀的用户界面设计
什么才是优秀的用户界面设计
旅行,写作,编程
旅行,写作,编程
软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有