这个SSM配置是经过优化的,省去了一些繁琐的配置,例如每次新建一个bean时都必须在配置mybitasSqlSessionFactoryBean里添加上一个classpath,在这套配置里省去了这一步,如何省去的,请看下面的配置文件。
1.配置文件中需要用到的jar包
2 .配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 加载spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring配置文件加载的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!-- 配置struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<!-- 需要拦截什么样的后缀名就写什么,本配置中拦截的后缀名是.action -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.none</url-pattern>
</filter-mapping>
</web-app>
3、配置spring配置文件applicationContext.xml,主要包括配置数据源、事务、mybaits等
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 采用注释的方式配置bean -->
<context:annotation-config />
<!-- 配置要扫描的包 -->
<context:component-scan base-package="com.company.project"></context:component-scan>
<!--proxy-target-class="true"强制使用cglib代理 如果为false则spring会自动选择-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 数据库配置文件位置 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 队列中的最小等待数 -->
<property name="minIdle" value="${jdbc.minIdle}"></property>
<!-- 队列中的最大等待数 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 最长等待时间,单位毫秒 -->
<property name="maxWait" value="${jdbc.maxWait}"></property>
<!-- 最大活跃数 -->
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="initialSize" value="${jdbc.initialSize}"></property>
</bean>
<!-- 配置mybitasSqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations">
<list>
<value>classpath:com/company/project/entity/sqlMapper/*.xml</value>
</list>
</property>
</bean>
<!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 事务配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 使用annotation注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4.JDBC的配置jdbc.properties(用的MySQL驱动)
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123456
jdbc.maxActive = 2
jdbc.maxIdle =5
jdbc.minIdle=1
jdbc.initialSize =3
jdbc.maxWait =3000
5.配置MyBatis的配置文件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>
<!-- changes from the defaults -->
<setting name="lazyLoadingEnabled" value="false" />
</settings>
<typeAliases>
<typeAlias alias="user" type="com.company.project.entity.User"/>
</typeAliases>
<!-- <mappers>
<mapper resource="com/company/project/entity/sqlMappers/user.xml" />
</mappers> -->
</configuration>
6.实体类User的写法
public class User implements Serializable {
private static final long serialVersionUID = -4415990281535582814L;
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
7.User映射文件配置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="com.company.project.entity.User">
<resultMap type="com.company.project.entity.User" id="userResult">
<result property="id" column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<result property="username" column="username" />
<result property="password" column="password" />
</resultMap>
<select id="userLogin" parameterType="user" resultMap="userResult">
select * from user
where
username=#{username} and password=#{password}
</select>
<select id="selectAllUser" resultMap="userResult">
select * from user
</select>
<select id="findById" parameterType="int" resultMap="userResult">
select *
from user where id=#{id}
</select>
<insert id="save" parameterType="user">
<![CDATA[
insert into
user(username,password) values(#{username},#{password})
]]>
</insert>
<update id="updateById" parameterType="user">
update user set
username=#{username},password=#{password} where id=#{id}
</update>
<delete id="deleteById" parameterType="int">
delete from user where
id=#{id}
</delete>
</mapper>
public interface UserDao {
public abstract void insert(User user);
public abstract void updateUser(User user);
public abstract void deleteUser(Integer userId);
public abstract User findUserByid(Integer userId);
public abstract List<User> findAll();
public abstract User userLogin(User user);
}
9.UserDao实现
package com.company.project.dao.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.company.project.dao.UserDao;
import com.company.project.entity.User;
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
@Override
public void insert(User user) {
sqlSessionTemplate.insert("insertUser", user);
}
@Override
public void updateUser(User user) {
sqlSessionTemplate.update("updateUser", user);
}
@Override
public void deleteUser(Integer userId) {
sqlSessionTemplate.delete("deleteUser", userId);
}
@Override
public User findUserByid(Integer userId) {
return sqlSessionTemplate.selectOne("findUserById", userId);
}
@Override
public List<User> findAll() {
return sqlSessionTemplate.selectList("selectAllUser");
}
@Override
public User userLogin(User user) {
return sqlSessionTemplate.selectOne("userLogin", user);
}
@Override
public int selectTotalRecord() {
return sqlSessionTemplate.selectOne("selectTotalRecord");
}
@Override
public List<User> pageSelect(int startRow, int endRow) {
Map<String, Integer> params =new HashMap<String, Integer>();
params.put("startRow", new Integer(startRow));
params.put("endRow", new Integer(endRow));
return sqlSessionTemplate.selectList("pageSelect", params);
}
}
10.struts.xml配置
<action name="user_*" class="com.company.project.action.UserAction" method="{1}">
<!-- <result name="success" type="redirectAction">user_queryAllUser.action</result>
<result name="input">/WEB-INF/index.jsp</result>
<result name="addUser">/WEB-INF/userAdd.jsp</result>
<result name="updateUser">/WEB-INF/userUpdate.jsp</result>
<result name="index">/WEB-INF/login.jsp</result> -->
<result name="newFile">/WEB-INF/page/NewFile.jsp</result>
<result name="index">/WEB-INF/page/login.jsp</result>
<result name="userList">/WEB-INF/page/userList.jsp</result>
</action>
<!-- <action name="login"
class="com.company.project.action.UserAction"
method="login">
<result name="success">
/WEB-INF/index.jsp
</result>
</action>
<action name="index"
class="com.company.project.action.UserAction"
method="index">
<result name="success">
/WEB-INF/login.jsp
</result>
</action> -->
</package>
<package name="json" namespace="/json" extends="json-default">
<global-results>
<!-- 此包类的Action类没指result属性的就全使用此属性 -->
<result type="json"/>
</global-results>
<action name="queryAllUser" class="com.company.project.action.UserAction" method="queryAllUser">
<result type="json">
<param name="root">userList</param>
</result>
</action>
</package>
</struts>