SSM整合:
导入依赖:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.itcast.parent</groupId>
<artifactId>itcast-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.itcast.usermanage</groupId>
<artifactId>itcast-usermanage</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<!-- Jackson Json处理工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- 时间操作组件 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<!-- Apache工具组件 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<!-- 分页助手 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
</dependency>
<!-- 文件上传组件 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8089</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
在web.xml中进行spring和springmvc的配置,需要的是springmvc的核心前端控制器和spring的核心监听器。
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="MyWebApp" version="2.5">
<display-name>mjf-usermanage</display-name>
<!-- 配置spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 编码过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 加载springmvc -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
还有就是各种配置文件,如mybatis-config.xml和UserMapper.xml还有springmvc-servlet.xml和applicationContext.xml
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 开启驼峰映射 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 配置分页助手 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 配置 dialect为mysql -->
<property name="dialect" value="mysql" />
<!-- 配置查询总记录 -->
<property name="rowBoundsWithCount" value="true" />
</plugin>
</plugins>
</configuration>
UserMapper.xml:
<?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="mjf.haihan.usermanager.mapper.UserMapper">
<!-- 查询所有用户 -->
<select id="queryAll" resultType="User">
select * from tb_user;
</select>
<!-- 添加用户 -->
<insert id="saveUser" useGeneratedKeys="true" keyColumn="id" keyProperty="id" >
insert into tb_user(
user_name,
password,
name,
age,
sex,
birthday
)
VALUES(
#{userName},
#{password},
#{name},
#{age},
#{sex},
#{birthday}
);
</insert>
<!-- 删除用户 -->
<delete id="deletUser">
delete from tb_user where id=#{id};
</delete>
<!-- 根据id查询用户 -->
<select id="queryById" resultType="User">
select * from tb_user where id = #{id};
</select>
<!-- 修改用户 -->
<update id="edit" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
update tb_user set
user_name = #{userName},
password = #{password},
name = #{name},
age = #{age},
sex = #{sex},
birthday = #{birthday}
where id = #{id}
</update>
</mapper>
Springmvc-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解驱动 -->
<mvc:annotation-driven />
<!-- 处理器 -->
<context:component-scan base-package="mjf.haihan.usermanager.controller" />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
applicationContext.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 使用spring自带的占位符替换功能 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 允许JVM参数覆盖 -->
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<!-- 忽略没有找到的资源文件 -->
<property name="ignoreResourceNotFound" value="true" />
<!-- 配置资源文件 -->
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置总包扫描 -->
<context:component-scan base-package="mjf.haihan.usermanager"/>
<bean class="com.jolbox.bonecp.BoneCPDataSource" id="dataSource">
<!-- 数据库驱动 -->
<property name="driverClass" value="${jdbc.driver}" />
<!-- 相应驱动的jdbcUrl -->
<property name="jdbcUrl" value="${jdbc.url}" />
<!-- 数据库的用户名 -->
<property name="username" value="${jdbc.username}" />
<!-- 数据库的密码 -->
<property name="password" value="${jdbc.password}" />
<!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
<property name="idleConnectionTestPeriod" value="60" />
<!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
<property name="idleMaxAge" value="30" />
<!-- 每个分区最大的连接数 -->
<property name="maxConnectionsPerPartition" value="150" />
<!-- 每个分区最小的连接数 -->
<property name="minConnectionsPerPartition" value="5" />
</bean>
<!-- 导入mybatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 加载全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<!-- 扫描目标下所有的mapper映射xml文件 -->
<property name="mapperLocations" value="classpath:mybatis/mapping/*.xml"/>
<!-- 扫描包下的注解的别名 -->
<property name="typeAliasesPackage" value="mjf.haihan.usermanager.pojo" />
</bean>
<!-- 无需指定sqlSessionFactory,扫描自动搜索查询sqlSessionFactory -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mjf.haihan.usermanager.mapper"/>
</bean>
</beans>
各种的配置准备好了,就是需要定义mvc三层开发结构了。
包括Action、Dao和Service层
Action:
package mjf.haihan.usermanager.controller;
import java.util.List;
import mjf.haihan.usermanager.pojo.User;
import mjf.haihan.usermanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@RequestMapping("user")
@Controller
public class UserAction {
@Autowired
private UserService userService;
@RequestMapping("users")
public ModelAndView queryAll(){
ModelAndView mv = new ModelAndView("users");
List<User> users = userService.queryAll();
mv.addObject("users", users);
return mv;
}
@RequestMapping("adduser")
public String saveUser(User user){
userService.saveUser(user);
return "redirect:/user/users";
}
@RequestMapping("deleteuser")
public String deleteuser(Long id){
userService.deleteUser(id);
return "redirect:/user/users";
}
@RequestMapping("edituser")
public ModelAndView editUser(Long id){
ModelAndView mv = new ModelAndView("edit");
User user = userService.queryById(id);
mv.addObject("user", user);
return mv;
}
@RequestMapping("edit")
public String edit(User user){
userService.edit(user);
return "forward:/user/users";
}
}
Dao\Mapper:
package mjf.haihan.usermanager.mapper;
import java.util.List;
import mjf.haihan.usermanager.pojo.User;
public interface UserMapper {
/**查询所有用户*/
public List<User> queryAll();
/**添加用户*/
public void saveUser(User user);
/**删除用户 */
public void deletUser(Long id);
/**跳转到修改页面 */
public User queryById(Long id);
/**修改用户*/
public void edit(User user);
}
Service:
package mjf.haihan.usermanager.service;
import java.util.Date;
import java.util.List;
import mjf.haihan.usermanager.mapper.UserMapper;
import mjf.haihan.usermanager.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> queryAll(){
List<User> users = userMapper.queryAll();
return users;
}
public void saveUser(User user){
user.setId(null);
user.setCreated(new Date());
user.setUpdated(user.getCreated());
userMapper.saveUser(user);
}
public void deleteUser(Long id) {
userMapper.deletUser(id);
}
public User queryById(Long id) {
User user = userMapper.queryById(id);
return user;
}
public void edit(User user) {
// TODO Auto-generated method stub
userMapper.edit(user);
}
}
这个是简单的用户crud的web项目,