标签:
一、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="WebApp_ID" version="2.5"> <display-name>ssm</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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:springMVC.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>PostEncoding</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>PostEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>changMethod</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>changMethod</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <display-name>MIPO_CRM</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
二、jdbc.properties(Mysql)
#config mySQLdriver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/crm user=root pwd=
三、spring.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 扫包 --> <context:component-scan base-package="com.wql.service"/> <context:component-scan base-package="com.wql.dao"/> <!-- 引用jdbc.properties --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${mySQLdriver}"/> <property name="url" value="${url}"/> <property name="username" value="${user}"/> <property name="password" value="${pwd}"/> </bean> <!--获取sqlSesssionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" > <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/wql/vo/*.xml"/> <property name="typeAliasesPackage" value="com.wql.vo"/> </bean> <!-- 获取SqlSessionTemplate --> <bean class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg ref="sqlSessionFactory"></constructor-arg> </bean> <!-- 上传文件拦截,设置最大上传文件大小 10M=10*1024*1024(B)=10485760 bytes --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760" /> </bean> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用事务的注解模式 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
四、springMVC.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: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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 扫包 --> <context:component-scan base-package="com.jun.controller"/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 访问静态资源 使用tomcat的默认的servlet--> <mvc:annotation-driven/> <mvc:resources location="/images/" mapping="/images/**"/> <mvc:resources location="/imgs/" mapping="/imgs/**"/> <mvc:resources location="/img/" mapping="/img/**"/> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/js/" mapping="/js/**"/> <mvc:resources location="/easyui/" mapping="/easyui/**"/> <mvc:resources location="/My97DatePicker/" mapping="/My97DatePicker/**"/> </beans>
下面贴一下模型层、服务层代码,看一下在模型层是如何通过注解模式获取spring.xml中的SqlSessionFactory实例的以及在服务层如何通过注解模式达到事务配置的。
1.模型层
BaseDaoImpl.java 从下面代码可看出在BaseDaoImpl类中是通过@Autowired标识符装载spring.xml中的SqlSessionFactory实例的。
public class BaseDaoImpl<T> { private Class clazz; public BaseDaoImpl(){ ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass(); clazz = (Class)type.getActualTypeArguments()[0]; } @Autowired protected SqlSessionTemplate session; /** * 添加 * @param obj */ public boolean insert(Object obj){ return session.insert(clazz.getName()+".insert" , obj)>0; } /** * 修改 */ public boolean update(Object obj){ return session.update(clazz.getName()+".update",obj)>0; } /** * 删除 */ public boolean delete(Serializable id){ return session.delete(clazz.getName()+".delete",id)>0; } /** * findbyId */ public T findById(Serializable id){ return session.selectOne(clazz.getName()+".findById", id); } /** * findAll */ public List<T> findAll(Map map){ return session.selectList(clazz.getName()+".findAll", map); } /** * list * @return */ public List<T> list(){ return session.selectList(clazz.getName()+".list"); } /** * 分页 * @param curPage 当前页 * @param pageRows 每页的条数 * @param map * @return */ public Page getPageData(long curPage , long pageRows ,Map map){ Page page = new Page(); long count = session.selectOne(clazz.getName()+".getCount",map);//获取数据的总条数 long allPage = (count+pageRows -1)/pageRows;//根据每页的行数+总条数获取总页数 //设置当前页 if(curPage < 1)curPage = 1;//如果当前页小于1则设当前页的值为1 if(allPage > 0 && curPage > allPage ) curPage = allPage;//如果当前页大于总页数则设当前页为总页数 map.put("start", (curPage -1)* pageRows);//设置查询的起始值 map.put("stop", pageRows);//设置返回的最大条数——即每页的行数 //获取数据 List<T> dataList = session.selectList(clazz.getName()+".getDataList",map); page.setCurPage(curPage);//当前页 page.setPageRows(pageRows);//每页的行数 page.setPageNum(allPage);//总页数 page.setDataList(dataList);//数据 return page; } }
OrdergoodDaoImpl.java
@Repository public class OrdergoodDaoImpl extends BaseDaoImpl<Ordergood>{ /** * 显示所有订单商品表(根据id查询) * @return */ public List<Ordergood> listOrdergood(Map map){ return super.findAll(map); } /** * 添加订单商品表 * @param ordergoods * @return */ public boolean addOrdergood(List<Ordergood> ordergoods){ return session.insert(Ordergood.class.getName()+".addOrdergood", ordergoods)>0; } }
2.服务层
FunctionService.java 从下面代码可看出在服务层是通过@Transactional标识符来配置事务的。
@Service @Transactional public class FunctionService { @Autowired private FunctionDaoImpl functionDao; /** * 所有的权限 * @return */ public List<Function> listFunction(){ return functionDao.listFunction(); } /** * 父权限查询 * @param map * @return */ public List<Function> listParentFunction(){ return functionDao.listParentFunction(); } /** * 子权限查询 * @param map * @return */ public List<Function> listChildFunction(int parentid){ return functionDao.listChildFunction(parentid); } }
标签:
原文地址:http://www.cnblogs.com/wql025/p/5006167.html