整合maven+spring+springmvc+mybatis实现ssm基础开发框架,封装通用增删查改api,提高程序开发效率
myeclipse创建maven项目:
把maven项目转成maven web项目:项目右键Properties
把WebRoot里面的东西移到src/main/webapp下面
打开pom.xml,加入如下,否则报错
项目缺少web.xml文件,在src/main/webapp/WEB-INF下加入web.xml文件即可
pom.xml管理ssm相关jar包
4.0.0
com.xe.demo
demo-ssm
0.0.1-SNAPSHOT
war
UTF-8
1.8
4.2.5.RELEASE
3.3.1
1.2.4
5.1.29
1.2.4
1.0.13
javax.servlet
javax.servlet-api
3.1.0
org.springframework
spring-context
org.springframework
spring-orm
org.springframework
spring-oxm
org.springframework
spring-jdbc
org.springframework
spring-tx
org.springframework
spring-web
org.springframework
spring-webmvc
org.springframework
spring-aop
org.aspectj
aspectjweaver
1.8.2
com.fasterxml.jackson.core
jackson-databind
2.4.6
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
${mybatis.spring.version}
com.github.pagehelper
pagehelper
4.1.6
mysql
mysql-connector-java
${mysql.version}
com.alibaba
fastjson
${fastjson.version}
com.alibaba
druid
${druid.version}
commons-fileupload
commons-fileupload
1.3
commons-collections
commons-collections
3.2.2
org.apache.commons
commons-lang3
3.1
log4j
log4j
1.2.17
org.springframework
spring-framework-bom
${spring.version}
pom
import
maven-compiler-plugin
1.8
org.apache.maven.plugins
maven-war-plugin
ssm
删除WebRoot目录,右键更新项目
创建项目包结构
到此项目基础结构创建完成
==================================================整合ssm==================================================
配置mybatis-config.xml:
spring管理配置文件spring.xml:
########################### 数据源配置 ###########################
#########本地数据库#########
#数据源驱动
mysql.driver=com.mysql.jdbc.Driver
mysql.username=root
mysql.password=xie8845233
mysql.url=jdbc:mysql://localhost/demo?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
log4j.rootLogger=DEBUG,CONSOLE,ERR_LOG
log4j.addivity.org.apache=false
#打印到控制台
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} -%-4r [%t] %-5p %x - %m%n
#输入日志到文件
log4j.appender.ERR_LOG=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERR_LOG.Threshold=ERROR
log4j.appender.ERR_LOG.File=${catalina.home}/logs/demo
log4j.appender.ERR_LOG.DatePattern='_'yyyyMMdd'.log'
log4j.appender.ERR_LOG.Encoding=utf-8
log4j.appender.ERR_LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.ERR_LOG.layout.ConversionPattern=[m6] %d{yyyy-MM-dd HH\:mm\:ss} %5p %c{1}\:%L \: %m%n
log4j.logger.org.apache=ERROR
log4j.logger.org.mybatis=ERROR
log4j.logger.org.springframework=ERROR
demo-bstable
index.jsp
contextConfigLocation
classpath:spring/spring.xml
log4jConfigLocation
classpath:log4j.properties
org.springframework.web.util.Log4jConfigListener
org.springframework.web.context.ContextLoaderListener
demoServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/spring-mvc.xml
1
demoServlet
/
default
*.html
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
package com.xe.demo.mapper;
import java.io.Serializable;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.xe.demo.common.pojo.PageAjax;
/**
* Mybatis基础DAO
* @author 蔡泽华
*/
public interface BaseMapper {
/**
* 通过ID查询
* @param id
* @return
*/
Object selectById(Serializable id);
/**
* 查询单条记录
* @param entity
* @return
*/
Object selectOne(@Param("item")Object obj);
/**
* 查询记录集合
* @param entity
* @return
*/
List> selectList(@Param("item")Object obj);
/**
* 分页查询
* @param t
* @param page
* @return
*/
List selectPage(@Param("item")Object obj, @Param("page")PageAjax page);
/**
* 通用的保存方法
* @param
* @param entity
*/
void save(@Param("item")Object obj);
/**
* 批量保存
* @param list
*/
int batchSave(List> list);
/**
* 通用的修改方法
* @param
* @param entity
*/
int update(@Param("item")Object obj);
/**
* 批量更新
* @param list
* @return
*/
int batchUpdate(List> list);
/**
* 删除方法
* @param id
*/
int delById(Serializable id);
/**
* 批量删除
* @param list
* @return
*/
int delList(List> list);
/**
* 批量删除方法
* @param ids
*/
int delArray(int[] ids);
/**
* 统计查询
* @param
* @param params 查询参数
* @return 总记录条数
*/
int count(Object obj);
}
创建通用业务层实现BaseService.java:
package com.xe.demo.service;
import java.io.Serializable;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.xe.demo.common.pojo.PageAjax;
import com.xe.demo.mapper.BaseMapper;
/**
* 通用业务层
* @author 蔡泽华
*/
public class BaseService {
@Autowired
private BaseMapper baseMapper;
/**
* 通过ID查询
* @param id
* @return
*/
public Object selectById(Serializable id) {
return baseMapper.selectById(id);
}
/**
* 查询单条记录
* @param entity
* @return
*/
public Object selectOne(Object obj) {
return baseMapper.selectOne(obj);
}
/**
* 查询集合
* @param t
* @return
*/
public List> selectList(Object obj) {
return (List>) baseMapper.selectList(obj);
}
/**
* 分页查询
* @param t
* @param page
* @return
*/
public PageAjax selectPage(Object obj, PageAjax page){
List list = baseMapper.selectPage(obj, page);
return new PageAjax(list);
}
/**
* 通用的保存方法
* @param
* @param entity
*/
public void save(Object obj) {
baseMapper.save(obj);
}
/**
* 批量保存
* @param list
*/
public void batchSave(List> list) {
baseMapper.batchSave(list);
}
/**
* 通用的修改方法
* @param
* @param entity
*/
public void update(Object obj) {
baseMapper.update(obj);
}
/**
* 删除方法
* @param id
*/
public int delById(Serializable id) {
return baseMapper.delById(id);
}
/**
* 批量删除
* @param list
* @return
*/
public int delList(List> list) {
return baseMapper.delList(list);
}
/**
* 批量删除方法
* @param ids
*/
public int delArray(int[] ids) {
return baseMapper.delArray(ids);
}
/**
* 统计查询
* @param
* @param params 查询参数
* @return 总记录条数
*/
public int count(T t) {
return baseMapper.count(t);
}
/**
* 批量更新
* @param list
* @return
*/
public int batchUpdate(List> list){
return baseMapper.batchUpdate(list);
}
/*@Autowired
private SqlSessionTemplate sqlSessionTemplate;
*//**
* 批量更新
* @param str
* @param obj
* @return
* @throws Exception
*//*
public int batchUpdate(String str, List> objs) {
SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
// 批量执行器
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
int succ = 0;
try {
if (objs != null) {
for (int i = 0, size = objs.size(); i < size; i++) {
sqlSession.update(str, objs.get(i));
}
sqlSession.flushStatements();
sqlSession.commit();
sqlSession.clearCache();
}
} finally {
sqlSession.close();
}
return succ;
}*/
}
CREATE TABLE `t_user` (
`id` int(3) unsigned NOT NULL AUTO_INCREMENT,
`username` char(20) NOT NULL,
`password` char(32) NOT NULL,
`email` varchar(100) DEFAULT NULL,
`useable` tinyint(4) NOT NULL DEFAULT '1' COMMENT '是否可用(0禁用,1可用)',
`addtime` datetime NOT NULL COMMENT '创建时间',
`logintime` datetime DEFAULT NULL COMMENT '登陆时间',
`loginip` varchar(15) DEFAULT NULL COMMENT '登陆IP',
PRIMARY KEY (`id`),
KEY `username` (`username`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8;
编写增删查改sql映射文件UserMapper.xml:
insert into t_user(username, password, email, useable, addtime)
values(#{username}, #{password}, #{email}, #{useable}, now())
update t_user set password = #{password}, email = #{email}, useable = #{useable} where id = #{id}
insert into t_user(username, password, email, useable, addtime) values
(#{item.username}, #{item.password}, #{item.email}, #{item.useable}, now())
update t_user set password = #{item.password}, email = #{item.email}, useable = #{item.useable} where id = #{item.id}
delete from t_user where id in
#{item}
delete from t_user where id in
#{item}
创建UserService继承BaseService.java;
创建UserController.java实现功能:
package com.xe.demo.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xe.demo.common.pojo.AjaxResult;
import com.xe.demo.common.pojo.PageAjax;
import com.xe.demo.model.User;
import com.xe.demo.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@RequestMapping("/queryById")
public AjaxResult queryById(int id){
// User user = userService.selectById(id);
Object user = userService.selectById(id);
return new AjaxResult(user);
}
@ResponseBody
@RequestMapping("/queryOne")
public AjaxResult queryOne(User user){
Object obj = userService.selectOne(user);
return new AjaxResult(obj);
}
@ResponseBody
@RequestMapping("/queryList")
public AjaxResult queryList(User user){
List> list = userService.selectList(user);
return new AjaxResult(list);
}
@ResponseBody
@RequestMapping("/queryPage")
public PageAjax queryPage(User user, PageAjax page){
return userService.selectPage(user, page);
}
@ResponseBody
@RequestMapping("/save")
public AjaxResult save(User user){
//保存map测试
Map map = new HashMap();
map.put("username", user.getUsername());
map.put("email", user.getEmail());
map.put("useable", user.getUseable());
map.put("password", user.getPassword());
userService.save(map);
return new AjaxResult();
}
@ResponseBody
@RequestMapping("/update")
public AjaxResult update(User user){
//更新map测试
// Map map = new HashMap();
// map.put("id", user.getId());
// map.put("email", user.getEmail());
// map.put("useable", user.getUseable());
// map.put("password", user.getPassword());
userService.update(user);
return new AjaxResult();
}
@ResponseBody
@RequestMapping("/batchSave")
public AjaxResult batchSave(){
// List list = new ArrayList();
// User user = null;
// for(int i = 0; i < 10; i ++){
// user = new User();
// String username = "ourte" + i;
// user.setUsername(username);
// user.setPassword("123456");
// user.setUseable(1);
// user.setEmail(username + "@163.com");
// list.add(user);
// }
//map批量保存
List
2.通过条件查询返回实体集合
3.通过条件查询返回Map集合
4.多条件分页查询实体
5.多条件分页查询map
6.批量保存数据
7.批量更新数据
8.评论删除(mybatis接收List
9.批量删除(mybatis接收int[]参数)
10.添加数据
11.更新数据
项目源码下载地址:整合maven+spring+springmvc+mybatis实现ssm通用增删查改API(没BUG版本)