最近在做一个小项目,基于Maven构建,框架使用现在主流的:SpringMVC(4.1)+Spring(4.1)+MyBatis(3.4.0),实现基本增删改查,页面国际化。数据源采用阿里开源的Druid,前端采用基于Bootstrap封装的模板AdminLTE(2.3.6)及Bootstrap的一些插件。MyBaits采用了另外一位博友提供的通用CRUD代码。
前段时间发过几篇文档,陆续有些朋友需要源码,今天有时间,将项目简化,源码发出。仅供学习!
文章地址:
http://blog.csdn.net/cyh1111/article/details/52961250
http://blog.csdn.net/cyh1111/article/details/52960233
1、登陆
2、注册
3、主页
4、Modal 框 修改
5、操作消息提醒–2秒自动关闭
1、Maven POM文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.cyhgroupId>
<artifactId>SSM-DemoartifactId>
<packaging>warpackaging>
<version>1.0.0.0version>
<name>SSM-Demoname>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<junit.version>4.11junit.version>
<jstl.version>1.2jstl.version>
<javaee-api.version>7.0javaee-api.version>
<cglib.version>3.2.2cglib.version>
<aspectjrt.version>1.8.0aspectjrt.version>
<aspectjweaver.version>1.8.0aspectjweaver.version>
<spring.version>4.1.7.RELEASEspring.version>
<mybatis-spring.version>1.3.0mybatis-spring.version>
<mybatis.version>3.4.0mybatis.version>
<log4j.version>1.2.17log4j.version>
<slf4j.version>1.7.21slf4j.version>
<fileupload.version>1.3.1fileupload.version>
<mysql.version>5.1.35mysql.version>
<druid.version>1.0.19druid.version>
<fastjson.version>1.2.11fastjson.version>
<gson.version>2.3.1gson.version>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
dependency>
<dependency>
<groupId>javaxgroupId>
<artifactId>javaee-apiartifactId>
<version>${javaee-api.version}version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>${jstl.version}version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>${aspectjweaver.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>${mybatis.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>${mybatis-spring.version}version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>${druid.version}version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>${fastjson.version}version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>${fileupload.version}version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poiartifactId>
<version>3.10-FINALversion>
dependency>
<dependency>
<groupId>com.google.zxinggroupId>
<artifactId>coreartifactId>
<version>3.3.0version>
dependency>
<dependency>
<groupId>com.google.code.gsongroupId>
<artifactId>gsonartifactId>
<version>${gson.version}version>
dependency>
<dependency>
<groupId>org.jsongroupId>
<artifactId>jsonartifactId>
<version>20160212version>
dependency>
dependencies>
<build>
<finalName>SSM-DemofinalName>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-deploy-pluginartifactId>
<version>2.7version>
<configuration>
<uniqueVersion>falseuniqueVersion>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-eclipse-pluginartifactId>
<version>2.8version>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.2version>
<configuration>
<failOnError>truefailOnError>
<verbose>trueverbose>
<fork>truefork>
<compilerArgument>-nowarncompilerArgument>
<source>1.7source>
<target>1.7target>
<encoding>UTF-8encoding>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-source-pluginartifactId>
<version>2.1.2version>
<executions>
<execution>
<id>attach-sourcesid>
<goals>
<goal>jargoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
2、SpringMVC国际、文件上传配置
文件上传原文:http://blog.csdn.net/cyh1111/article/details/52960233
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.cyh.*">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
context:component-scan>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages/message"/>
<property name="useCodeAsDefaultMessage" value="true" />
bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
bean>
<mvc:resources location="/res/" mapping="/res/**"/>
<mvc:resources location="/WEB-INF/pages/error/" mapping="/WEB-INF/pages/error/**"/>
<mvc:resources location="/upload/" mapping="/upload/**"/>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean id="multipartResolver" class="com.cyh.sy.web.upload.CustomMultipartResolver"/>
beans>
3、Spring+Mybatis+Druid集成配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<context:property-placeholder location="classpath:config.properties"/>
<bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource" destroy-method = "close">
<property name = "url" value = "${druid.url}" />
<property name = "username" value = "${druid.username}" />
<property name = "password" value = "${druid.password}" />
<property name = "driverClassName" value = "${druid.driverClassName}" />
<property name = "filters" value = "${druid.filters}" />
<property name = "maxActive" value = "${druid.maxActive}" />
<property name = "initialSize" value = "${druid.initialSize}" />
<property name = "maxWait" value = "${druid.maxWait}" />
<property name = "minIdle" value = "${druid.minIdle}" />
<property name = "timeBetweenEvictionRunsMillis" value ="${druid.timeBetweenEvictionRunsMillis}" />
<property name = "minEvictableIdleTimeMillis" value ="${druid.minEvictableIdleTimeMillis}" />
<property name = "validationQuery" value = "${druid.validationQuery}" />
<property name = "testWhileIdle" value = "${druid.testWhileIdle}" />
<property name = "testOnBorrow" value = "${druid.testOnBorrow}" />
<property name = "testOnReturn" value = "${druid.testOnReturn}" />
<property name = "maxOpenPreparedStatements" value ="${druid.maxOpenPreparedStatements}" />
<property name = "removeAbandoned" value = "${druid.removeAbandoned}" />
<property name = "removeAbandonedTimeout" value ="${druid.removeAbandonedTimeout}" />
<property name = "logAbandoned" value = "${druid.logAbandoned}" />
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config/mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:mybatis-config/mapper/*-mapper.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cyh.sy.dao"/>
bean>
<bean name="sqlSessionTemplateASS" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="close">
<constructor-arg index="0" ref="sqlSessionFactory" />
bean>
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
bean>
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="find*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="select*" read-only="true" />
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.cyh.sy.service.*.*(..))" />
<aop:advisor pointcut-ref="pc" advice-ref="tx" />
aop:config>
<context:component-scan base-package="com.cyh.*">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>
beans>
4、Mybaits通用CRUD
原文见这里:http://blog.csdn.net/guoshengkai373/article/details/51461242
5、注册邮件激活
/**
* @描述:邮件发送工具类
* @作者:cyh
* @版本:V1.0
* @创建时间::2016-11-21 下午10:47:36
*/
public class EmailUtils {
private static final String FROM = "[email protected]";
private static final String PASS_WORD = "test123";
/**
* 注册成功后,向用户发送帐户激活链接的邮件
* @param user 未激活的用户
*/
public static void sendAccountActivateEmail(Company company) {
Session session = getSession();
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(FROM));
message.setSentDate(new Date());
message.setSubject("SSM-Demo系统-帐户激活邮件");
message.setRecipient(RecipientType.TO, new InternetAddress(company.getCompanyContactsEmail()));
message.setContent("此邮件为:SSM-Demo系统官方激活邮件!请点击下面链接完成激活操作!
" +
"点击激活帐户
","text/html;charset=UTF-8");
message.saveChanges();
// 发送邮件
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发送重设密码链接的邮件
*/
public static void sendResetPasswordEmail(Company company) {
Session session = getSession();
MimeMessage message = new MimeMessage(session);
try {
message.setSubject("SSM-Demo系统-找回您的帐户与密码");
message.setSentDate(new Date());
message.setFrom(new InternetAddress(FROM));
message.setRecipient(RecipientType.TO, new InternetAddress(company.getCompanyContactsEmail()));
message.setContent("此邮件为:SSM-Demo系统官方账户与密码找回邮件!请点击下面链接重新设置密码!
" +
"点击重新设置密码
","text/html;charset=UTF-8");
message.saveChanges();
// 发送邮件
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Session getSession() {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.163.com");
props.setProperty("mail.smtp.port", "25");
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(FROM, PASS_WORD);
}
});
return session;
}
6、页面Datatables实现表格服务端分页、条件查询
http://blog.csdn.net/cyh1111/article/details/52961250
7、toastr消息提醒
使用非常简单,不同消息不同颜色提示!
<link rel="stylesheet" href="<%=path%>/res/toastr/toastr.min.css">
<script src="<%=path%>/res/toastr/toastr.min.js">script>
<script type="text/javascript">
script>
toastr.success("操作成功!");
toastr.error("操作失败!");
8、其他功能:文件打包压缩、二维码生成、json序列化、sweetalert消息提醒等等
数据库脚本:\SSM-Demo\src\main\bin\ssm-demo.sql
CSDN下载:下载
GitHub:下载