本示例工程整合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是否正确设置。
相关推荐
Spring Boot 是一种用于简化 Spring 应用程序的初始搭建以及开发过程的框架。它使用特定的方式配置 ...通过这些步骤,可以快速搭建起一个 Spring Boot 项目,然后在此基础上添加业务逻辑和定制化的配置来满足应用需求。
Spring Boot 是一个由 Pivotal 开发的框架,旨在简化 Spring 应用程序的初始搭建以及开发过程。它集成了大量的常用第三方库配置,如 JDBC、MongoDB、JPA、RabbitMQ、Quartz 等,使得开发者可以快速地创建一个独立...
本知识点将深入探讨如何使用Maven来搭建一个多模块的SSM(Spring、Struts、MyBatis)框架,帮助开发者实现更有序、更高效的项目结构。 1. Maven基础: Maven是Apache基金会的一个项目,它通过一个项目对象模型...
搭建怎样一个环境 开发环境 导入快速启动项目 集成前准备 集成Mybatis 集成Swagger2 多环境配置 多环境下的日志配置 jwt配置 常用配置 为什么使用SpringBoot SpringBoot相对于传统的SSM框架的优点是提供了默认的样板...
SSM + Maven聚合工程的搭建降低了开发复杂度,使得团队成员可以快速地理解和参与到项目中,提高了协同开发的效率。在实际开发过程中,开发者还可以结合其他工具和库,如Spring Boot、Spring Cloud等,进一步提升项目...
本项目旨在构建一个完整的用户管理系统,该系统采用前后端分离的设计模式,前端采用React框架,后端则基于SSM(Spring、SpringMVC、MyBatis)技术栈。 **目标受众:** 1. **计算机专业学生:**希望通过实践全栈...
SSM-CRM项目是一个基于Java技术栈的企业级客户关系管理系统,使用了Spring、SpringMVC和MyBatis三个核心框架的组合,因此得名"SSM"。该项目提供了完整的数据库支持,用户可以直接运行,无需额外配置,这为快速搭建和...
Spring Boot 是一个简化Spring应用初始搭建以及开发过程的框架,它强调“约定优于配置”,使得开发者能够快速创建独立运行的、生产级别的基于Spring的应用。 在Spring Boot 2中,XML配置不再是首选,而是推荐使用...
- **Spring Boot**:尽管标题未提及,但SSM框架在现代开发中常与Spring Boot结合使用,Spring Boot简化了Spring应用的初始搭建以及配置,使得开发更快速、更便捷。 - **SSM**:Spring、Spring MVC和MyBatis的组合,...
Spring Boot简化了SSM的集成,使得开发者可以快速搭建应用,同时具备自动配置和运行时健康检查等功能。此外,该项目还涉及到微信小程序的开发,可能提供了移动端的访问入口,便于用户随时随地查看和管理项目进度。 ...
逆向工程可以帮助开发者快速搭建数据访问层,方便地进行CRUD操作。 这些资料涵盖了SSH和SSM框架的基础知识、实战技巧以及MyBatis逆向工程的使用方法,对于Java Web开发者来说是一份宝贵的资源。通过学习和实践,...
4. **Spring Boot**:Spring Boot是Spring生态系统的简化启动器,旨在简化新Spring应用的初始搭建以及开发过程。它默认配置了很多常见的设置,如服务器端口、日志、数据源等,使得开发者可以快速上手。Spring Boot与...
5. **开发流程**:首先,搭建SSM环境,配置Spring、SpringMVC和Mybatis的相关XML文件或使用Spring Boot自动配置;接着,设计数据库表结构并编写Mybatis的Mapper接口及XML映射文件;然后,创建Service层和DAO层,实现...
【标题】"基于SSM的计算机知识竞赛网站"是一个典型的Java Web项目,它利用Spring、SpringMVC和MyBatis(SSM)三大框架构建,旨在实现一个在线的知识竞赛平台。这个项目可能包含了完整的前后端功能,允许用户参与竞赛...
7. **Java springboot**:虽然标题没有明确提及Spring Boot,但因为Spring Boot是Spring的简化版本,可以快速搭建应用,所以这个项目可能也使用了Spring Boot,它简化了配置,集成了大量常用组件,如数据源、日志、...
3. **SpringBoot**:Spring Boot是Spring框架的一个扩展,用于快速搭建和运行微服务应用。它通过自动配置和起步依赖简化了项目初始化和配置过程。 4. **SSM**:Spring、SpringMVC和MyBatis的组合,是Java Web开发中...
"Java springboot ssm"则表明后端使用了Spring Boot和SSM框架,这些是Java企业级应用开发的常用工具,能快速搭建稳定高效的服务器端结构。 **详细知识点:** 1. **Spring框架**:Spring是一个全面的Java应用程序...
"基于Vue+ssm框架的汇美食电子商城"是一个综合性的项目,它结合了前端的Vue.js框架和后端的SSM(Spring、SpringMVC、MyBatis)框架,旨在构建一个在线的美食购物平台。Vue.js是轻量级的JavaScript库,用于构建用户...
【标题】"基于SSM的宠物领养系统"是一个典型的Java Web项目,它结合了Spring、SpringMVC和MyBatis三个框架,用于构建一个功能完善的宠物领养平台。这样的系统通常包括用户注册、登录、发布和查看宠物信息、领养申请...