标签:分享图片 有关 seda center false 文件 iss 集合 回顾
上一篇我们创建了工程和一个Factory的po对象(javaBean),我们也写好了Mapper的映射文件,接下来我们来完成生产厂家的DAO与SERVICE,以及CONTROLLER,还有做显示的JSP页面,以及框架的配置文件。
首先回顾一下我们的系统架构:
我们的BaseDao与BaseDaoImpl:
- package cn.hpu.jk.dao;
-
- import java.io.Serializable;
- import java.util.List;
- import java.util.Map;
-
- import cn.hpu.jk.pagination.Page;
-
- public interface BaseDao<T> {
- public List<T> findPage(Page page);
- public List<T> find(Map paraMap);
- public T get(Serializable id);
- public void insert(T entity);
- public void update(T entity);
- public void deleteById(Serializable id);
- public void delete(Serializable[] ids);
- }
BaseDaoImpl:
- package cn.hpu.jk.dao.impl;
-
- import java.io.Serializable;
- import java.util.List;
- import java.util.Map;
-
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.mybatis.spring.support.SqlSessionDaoSupport;
- import org.springframework.beans.factory.annotation.Autowired;
-
- import cn.hpu.jk.dao.BaseDao;
- import cn.hpu.jk.pagination.Page;
-
-
- public abstract class BaseDaoImpl<T> extends SqlSessionDaoSupport implements BaseDao<T>{
- @Autowired
-
- public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
- super.setSqlSessionFactory(sqlSessionFactory);
- }
-
- private String ns;
- public String getNs() {
- return ns;
- }
- public void setNs(String ns) {
- this.ns = ns;
- }
-
- public List<T> findPage(Page page){
- List<T> oList = this.getSqlSession().selectList(ns + ".findPage", page);
- return oList;
- }
-
-
- public List<T> find(Map map) {
- List<T> oList = this.getSqlSession().selectList(ns + ".find", map);
- return oList;
- }
- public T get(Serializable id) {
- return this.getSqlSession().selectOne(ns + ".get", id);
- }
-
-
- public void insert(T entity) {
- this.getSqlSession().insert(ns + ".insert", entity);
- }
-
-
- public void update(T entity) {
- this.getSqlSession().update(ns + ".update", entity);
- }
-
-
- public void deleteById(Serializable id) {
- this.getSqlSession().delete(ns + ".deleteById", id);
- }
-
-
- public void delete(Serializable[] ids) {
- this.getSqlSession().delete(ns + ".delete", ids);
- }
- }
我们现在来创建自己的Dao层
FactoryDao:
- package cn.hpu.jk.dao;
-
- import cn.hpu.jk.domain.Factory;
-
- public interface FactoryDao extends BaseDao<Factory>{
-
-
-
- }
FactoryDaoImpl:
- package cn.hpu.jk.dao.impl;
-
- import org.springframework.stereotype.Repository;
-
- import cn.hpu.jk.dao.FactoryDao;
- import cn.hpu.jk.domain.Factory;
-
-
- @Repository
- public class FactoryDaoImpl extends BaseDaoImpl<Factory> implements FactoryDao{
- public FactoryDaoImpl(){
-
- super.setNs("cn.hpu.jk.mapper.FactoryMapper");
- }
- }
接下来写service层
FactoryService:
- package cn.hpu.jk.service;
-
- import java.io.Serializable;
- import java.util.List;
- import java.util.Map;
-
- import org.apache.poi.ss.formula.functions.T;
-
- import cn.hpu.jk.domain.Factory;
- import cn.hpu.jk.pagination.Page;
-
- public interface FactoryService {
- public List<Factory> findPage(Page page);
- public List<Factory> find(Map paraMap);
- public Factory get(Serializable id);
- public void insert(Factory factory);
- public void update(Factory factory);
- public void deleteById(Serializable id);
- public void delete(Serializable[] ids);
- }
FactoryServiceImpl(我们先只实现查询)
- package cn.hpu.jk.service.impl;
-
- import java.io.Serializable;
- import java.util.List;
- import java.util.Map;
-
- import javax.annotation.Resource;
-
- import org.apache.poi.ss.formula.functions.T;
- import org.springframework.stereotype.Service;
-
- import cn.hpu.jk.dao.FactoryDao;
- import cn.hpu.jk.domain.Factory;
- import cn.hpu.jk.pagination.Page;
- import cn.hpu.jk.service.FactoryService;
-
- @Service
- public class FactoryServiceImpl implements FactoryService{
-
- @Autowired
- FactoryDao factoryDao;
-
-
-
- @Override
- public void delete(Serializable[] ids) {
-
-
- }
-
-
- @Override
- public void deleteById(Serializable id) {
-
-
- }
-
-
- @Override
- public List<Factory> find(Map paraMap) {
- return factoryDao.find(paraMap);
- }
-
-
- @Override
- public List<Factory> findPage(Page page) {
-
- return null;
- }
-
-
- @Override
- public Factory get(Serializable id) {
-
- return null;
- }
-
-
- @Override
- public void insert(Factory factory) {
-
-
- }
-
-
- @Override
- public void update(Factory factory) {
-
-
- }
-
-
- }
下面开始编写Controller层
看看我们之前引入的工具类BaseController:
- package cn.hpu.jk.controller;
-
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- import org.springframework.beans.propertyeditors.CustomDateEditor;
- import org.springframework.web.bind.WebDataBinder;
- import org.springframework.web.bind.annotation.InitBinder;
-
-
- public abstract class BaseController {
- @InitBinder
-
- public void initBinder(WebDataBinder binder) {
- DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
- dateFormat.setLenient(true);
- binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
- }
- }
然后我们创建FactoryController(继承BaseController是为了以后的扩充):
- package cn.hpu.jk.controller.basicinfo.factory;
-
- import java.util.List;
-
- import javax.annotation.Resource;
-
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import cn.hpu.jk.controller.BaseController;
- import cn.hpu.jk.domain.Factory;
- import cn.hpu.jk.service.FactoryService;
-
-
- public class FactoryController extends BaseController{
- @Resource
- FactoryService factoryService;
-
-
- @RequestMapping("/basicinfo/factory/list.action")
- public String list(Model model){
- List<Factory> dataList=factoryService.find(null);
- model.addAttribute("dataList", dataList);
-
- return "/baseinfo/factory/jFactoryList.jsp";
- }
- }
接下来我们实现jsp界面
我们首先先在main/webapp/WEB-INF/pages下写一个最最基础的jsp的配置页面
base.jsp
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <c:set var="ctx" value="${pageContext.request.contextPath}"/>
-
-
- <link rel="stylesheet" rev="stylesheet" type="text/css" href="${ctx}/skin/default/css/default.css" media="all"/>
- <script language="javascript" src="${ctx}/js/common.js"></script>
之后再写一个显示list列表的配置jsp文件:
baselist.jsp
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%@ include file="base.jsp"%>
-
-
- <link rel="stylesheet" rev="stylesheet" type="text/css" href="${ctx}/css/extreme/extremecomponents.css" />
- <link rel="stylesheet" rev="stylesheet" type="text/css" href="${ctx}/css/extreme/extremesite.css" />
最后,,我们在main/webapp/WEB-INF/pages/factory下创建baseinfo文件夹,我们在里面写我们需要给用户显示的jsp界面(里面包含了baselist.jsp)
jFactoryList.jsp:
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%@ include file="../../baselist.jsp"%>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>生产厂家列表</title>
- </head>
-
-
- <body>
- <form name="icform" method="post">
-
-
- <div id="menubar">
- <div id="middleMenubar">
- <div id="innerMenubar">
- <div id="navMenubar">
-
- <ul>
- <li id="view"><a href="#" onclick="formSubmit(‘toview.action‘,‘_self‘);this.blur();">查看</a></li>
- </ul>
-
-
- </div>
- </div>
- </div>
- </div>
-
- <div class="textbox" id="centerTextbox">
- <div class="textbox-header">
- <div class="textbox-inner-header">
- <div class="textbox-title">
- 生产厂家列表
- </div>
- </div>
- </div>
-
- <div>
- <div class="eXtremeTable" >
- <table id="ec_table" class="tableRegion" width="98%" >
- <thead>
- <tr>
- <td class="tableHeader"><input type="checkbox" name="selid" onclick="checkAll(‘id‘,this)"></td>
- <td class="tableHeader">序号</td>
- <td class="tableHeader">厂家全称</td>
- <td class="tableHeader">缩写</td>
- <td class="tableHeader">联系人</td>
- <td class="tableHeader">电话</td>
- <td class="tableHeader">手机</td>
- <td class="tableHeader">传真</td>
- <td class="tableHeader">验货员</td>
- </tr>
- </thead>
- <tbody class="tableBody" >
-
- <c:forEach items="${dataList}" var="o" varStatus="status">
- <tr class="odd" onmouseover="this.className=‘highlight‘" onmouseout="this.className=‘odd‘" >
- <td><input type="checkbox" name="id" value="${o.id}"/></td>
- <td>${status.index+1}</td>
- <td><a href="toview.action?id=${o.id}">${o.fullName}</a></td>
- <td>${o.factoryName}</td>
- <td>${o.contacts}</td>
- <td>${o.phone}</td>
- <td>${o.mobile}</td>
- <td>${o.fax}</td>
- <td>${o.inspector}</td>
- </tr>
- </c:forEach>
-
- </tbody>
- </table>
- </div>
-
- </div>
-
-
- </form>
- </body>
- </html>
接下来进行最后一步,我们要配置各种配置文件
我们现在src/main/resources下配置MyBatis的配置文件
sqlMapConfig.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>
- <!-- 引入了spring就不需要这些了
- <environments default="">
- <environment id="">
- <transactionManager type=""></transactionManager>
- <dataSource type=""></dataSource>
- </environment>
- </environments>
- -->
- </configuration>
接下来我们创建spring的配置文件beans.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:mvc="http://www.springframework.org/schema/mvc"
- 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/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-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 ">
-
-
- <context:property-placeholder location="classpath:jdbc.properties"/>
-
-
- <context:component-scan base-package="cn.hpu.jk.dao,cn.hpu.jk.service"/>
-
-
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="${jdbc.driverClassName}"/>
- <property name="jdbcUrl" value="${jdbc.url}"/>
- <property name="user" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.password}"/>
-
- <property name="maxPoolSize" value="${c3p0.pool.maxPoolSize}"/>
- <property name="minPoolSize" value="${c3p0.pool.minPoolSize}" />
- <property name="initialPoolSize" value="${c3p0.pool.initialPoolSize}"/>
- <property name="acquireIncrement" value="${c3p0.pool.acquireIncrement}"/>
- </bean>
-
-
- <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
-
- <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
- <property name="mapperLocations" value="classpath:cn/hpu/jk/mapper/*.xml"></property>
- </bean>
-
-
- <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- </bean>
-
-
- <tx:advice id="txAdivce" transaction-manager="txManager">
- <tx:attributes>
- <tx:method name="insert*" propagation="REQUIRED"/>
- <tx:method name="update*" propagation="REQUIRED"/>
- <tx:method name="delete*" propagation="REQUIRED"/>
- <tx:method name="save*" propagation="REQUIRED"/>
-
- <tx:method name="find*" read-only="false"/>
- <tx:method name="get*" read-only="false"/>
- <tx:method name="view*" read-only="false"/>
- </tx:attributes>
- </tx:advice>
-
- <aop:config>
- <aop:pointcut expression="execution(* cn.hpu.jk.service.*.*(..))" id="txPointcut"/>
- <aop:advisor advice-ref="txAdivce" pointcut-ref="txPointcut"/>
- </aop:config>
-
- </beans>
其中jdbc.properties:
- #jdbc.driverClassName=com.mysql.jdbc.Driver
- #jdbc.url=jdbc:mysql://localhost:3306/jkdb?characterEncoding=utf-8
- #jdbc.username=root
- #jdbc.password=1234
-
-
- jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
- jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:XE
- jdbc.username=jkdb
- jdbc.password=22
-
-
- c3p0.pool.maxPoolSize=20
- c3p0.pool.minPoolSize=5
- c3p0.pool.initialPoolSize=3
- c3p0.pool.acquireIncrement=2
然后我们的spring有关service的配置文件
beans-service.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:mvc="http://www.springframework.org/schema/mvc"
- 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/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-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 ">
-
-
- <bean name="factoryService" class="cn.hpu.jk.service.impl.FactoryServiceImpl"/>
-
-
- </beans>
接下来配置springmvc的配置文件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:mvc="http://www.springframework.org/schema/mvc"
- 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/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-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 ">
-
-
- <context:component-scan base-package="cn.hpu.jk.controller.*"/>
-
-
- <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/pages"/>
- <property name="suffix" value=""/>
- </bean>
-
-
- <mvc:annotation-driven/>
-
-
-
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <property name="maxUploadSize" value="10485760"/>
- </bean>
-
- </beans>
最后我们来配置我们的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"
- xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- id="WebApp_ID" version="3.0">
-
-
- <display-name>Archetype Created Web Application</display-name>
-
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:beans*.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-servlet.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <url-pattern>*.action</url-pattern>
- </servlet-mapping>
-
-
- <filter>
- <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- </web-app>
我们基本上把所有基本的东西都配置完毕了,接下来我们发布我们的工程进行测试。
将任务部署至tomcat,然后我们在浏览器上访问我们的工程,我们访问我们的路径,看看数据是否返回(我们事先在数据库中填写了一些从测试数据):
访问http://localhost/jx-Maven-Webapp/(我的tomcat默认端口是80)
点击登录到主界面,然后点击厂家信息
点击之后结果如图:
【springmvc+mybatis项目实战】杰信商贸-5.生产厂家DAO+SERVICE+CONTROLLER+JSP+配置文件
标签:分享图片 有关 seda center false 文件 iss 集合 回顾
原文地址:https://www.cnblogs.com/yxllovetm/p/8807512.html