Srping Mvc maven web

一、src/main/resources/spring

1、spring-context-bean.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: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-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >
<bean id="custDao" class="com.cust.mapper.impl.CustDaoImpl">
<property name="sqlSession" ref="sqlSession" />
</bean>
<bean id="custService" class="com.cust.service.impl.CustServiceImpl">
<property name="custDao" ref="custDao" />
</bean>
</beans>

2\  spring-context.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:jdbc="http://www.springframework.org/schema/jdbc"  
xmlns:jee="http://www.springframework.org/schema/jee" 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.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
default-lazy-init="true">

<description>Spring Configuration</description>
<!-- jdbc -->
<context:property-placeholder location="classpath:/jdbc.properties" />

<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
    
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
   <property name="configLocation" value="classpath:/mybatis-config.xml" />  
   <property name="dataSource" ref="dataSource" />  
</bean>  

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sessionFactory" />
</bean>

    <bean id="jsonConverter" 
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  <property name="supportedMediaTypes" value="application/json" />
    </bean> 
</beans>

3\springMVC-servlet

<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<description>Spring Configuration</description>

<context:property-placeholder location="classpath:/jdbc.properties" />
<mvc:resources mapping="/javascript/**" location="/javascript/**" />

    <context:component-scan base-package="com.cust"/>
    <mvc:annotation-driven/>

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view"/>
<property name="suffix" value=".jsp"/>
   </bean>
</beans>

二、src/main/resources/

1\jdbc.properties

jdbc.type=mysql
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

2\log4j.properties

log4j.rootLogger=DEBUG,Console,File

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=[%c]%m%n

log4j.appender.File=org.apache.log4j.RollingFileAppender 
log4j.appender.File.File=mybatis.log
log4j.appender.File.MaxFileSize=10MB
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n

3\mybatis-config

<?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>
    <typeAliases>
<typeAlias type="com.cust.dto.Customer" alias="Customer" />
</typeAliases>
    <mappers>
        <mapper resource="com/cust/mapper/custMapper.xml"/>
    </mappers>
</configuration>

三、JSP

1\addCust.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>addcust</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<script type="text/javascript" src="/testCustWeb/javascript/jquery/jquery-1.11.1.min.js"></script>
<script type="text/javascript">

function guid() {
   function S4() {
      return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
   }
   return (S4()+S4()+S4()+S4()); 
}

var pk=guid();
$(document).ready(function(){
$("#custId").val(pk);
});

function save(){
$("#addForm").submit();
}
</script>
  </head>
  
  <body>
    <form id="addForm" action="cust/saveCust" method="post">
  <table id="contentTable" border="1px;" align="center">
<thead>
<tr><th id="tableTitle" colspan="2">添加客户</th></tr>
</thead>
<tbody>
<tr>
   <th>用户ID</th>
<td><input type="text" name="custId" id="custId" readonly="true"></td>
</tr>
<tr>
<th>用户名称</th>
<td><input type="text" name="custName" id="custName"/></td>
</tr>
<tr>
<th>用户密码</th>
<td><input type="password" name="address" id="address"></td>
</tr>
<tr>
<th>操作</th>
<td align="center"><input type="reset" value="重置"><input type="button" value="保存"  οnclick="save();"></td>
</tr>
</tbody>
</table>
    </form>
  </body>
</html>

2\custList.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Customer</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    

<script type="text/javascript" src="<%=basePath%>javascript/jquery/jquery-1.11.1.min.js"></script>
<script type="text/javascript">

function openwin(url,title)

  window.open(url,title,"height=100, width=400, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no");


function add(object){
var href="view/cust/addCust.jsp";
object.href=href;
}

function update(id,object){
var href="cust/initUpdate?custId="+id;
object.href=href;
}

function deleteC(id,object){

var href="cust/deleteCust?custId="+id;
object.href=href;
}



</script>
  </head>
  
  <body>
        <table id="custTable" border="1px;" align="left">
<thead>
<tr id="tableColumn">
<th>客户ID</th>
<th>客户名称</th>
<th>地址</th>
<th>操作</th></tr>
</thead>
<tbody>
<c:forEach items="${custL}" var="cust">
<tr>
<td>${cust.custId}</td>
<td>${cust.custName}</td>
<td>${cust.address}</td>
<td><a href="javascript:void(0);" οnclick="add(this);">添加</a>&nbsp;
<a href="javascript:void(0);" οnclick="deleteC('${cust.custId}',this)">删除</a>&nbsp;
<a href="javascript:void(0);" οnclick="update('${cust.custId}',this)">修改</a>&nbsp;
</td>
</tr>
</c:forEach>
</tbody>
</table>
  </body>
</html>

四、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>testCustWeb</display-name>
 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:/spring/spring-context*.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
<!-- 加载log4j 默认路径classpath-->
<context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>classpath:/log4j.properties</param-value>
</context-param>
    
    <filter>
        <filter-name>encodingFilter</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <servlet>
   <servlet-name>springServlet</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>springServlet</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
 
  
</web-app>

五、java

1\CustController

package com.cust.index.controller;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import com.cust.dto.Customer;
import com.cust.service.CustService;


@Controller
@RequestMapping("/cust/")
public class CustController {

@Autowired
private CustService custService;


@RequestMapping("/getList")
@ResponseBody
public List<Customer> getList(){
return custService.selectCustList();
}


@RequestMapping("/custList")
public String   custList(Model model){
List<Customer> custL=custService.selectCustList();
model.addAttribute("custL", custL);
   return "/cust/custList";
}

@RequestMapping("/initUpdate")
public String updateCust(String custId,Model model){
Customer cust=custService.selectById(custId);
model.addAttribute("cust",cust);
return "/cust/update";
}

@RequestMapping("/deleteCust")
public ModelAndView deleteCust(String custId,ModelAndView model){
custService.delete(custId);
model=getCustList();
return model;
}

@RequestMapping(value="/saveCust",method = RequestMethod.POST)
public ModelAndView add(@ModelAttribute Customer cust,ModelAndView model){
custService.save(cust);
model=getCustList();
return model;
}

@RequestMapping("/getCustList")
public ModelAndView  getCustList(){
List<Customer> custL=custService.selectCustList();
ModelAndView model=new ModelAndView("/cust/custList");
model.addObject("custL", custL);
   return model;
   
}

public CustService getCustService() {
return custService;
}


public void setCustService(CustService custService) {
this.custService = custService;
}



}

2\custMapper

<?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="cust">
<resultMap type="Customer" id="resultCust">
  <result property="custId" column="cust_id"/>
    <result property="custName" column="cust_name"/>
    <result property="address" column="address"/>
</resultMap>  
    <select id="selectCustList"  resultMap="resultCust"  parameterType="String">
        select * from cust
    </select>
    
    <select id="selectById" resultType="Customer" resultMap="resultCust"  parameterType="String">
        select * from cust where 1=1
        <if test=" _parameter != null">
        and cust_id= #{_parameter}
        </if>
    </select>
    
     <delete id="deleteById"   parameterType="String">
        <if test=" _parameter != null">
            delete from cust where cust_id= #{_parameter}
        </if>
    </delete>
    
    <insert id="insert" parameterType="Customer">
    insert into cust(cust_id,cust_name,address)
    values(
    #{custId},#{custName},#{address}
    )
    </insert>
    
    <update id="update" parameterType="Customer">
    update cust set cust_name=#{custName},address=#{address}
    where cust_id=#{custId}
    </update>
    
</mapper>

3\CustDaoImpl

package com.cust.mapper.impl;


import java.util.List;


import org.apache.ibatis.session.SqlSession;


import com.cust.dto.Customer;
import com.cust.mapper.CustDao;


public class CustDaoImpl implements CustDao{
private SqlSession sqlSession;
public SqlSession getSqlSession() {
return sqlSession;
}

public void setSqlSession(SqlSession sqlSession) {
this.sqlSession = sqlSession;
}

public List<Customer> selectCustList() {
// TODO Auto-generated method stub
List<Customer> userList=sqlSession.selectList("cust.selectCustList");
return userList;
}
public Customer selectById(String id) {
// TODO Auto-generated method stub
Customer cust=this.sqlSession.selectOne("cust.selectById",id);
return cust;
}
public int delete(String id) {
// TODO Auto-generated method stub
int resutl =this.sqlSession.delete("cust.deleteById",id);
return resutl;
}
public int save(Customer cust) {
int result=sqlSession.insert("cust.insert", cust);
return result;
}
public int update(Customer cust) {
// TODO Auto-generated method stub
int result=sqlSession.update("cust.update", cust);
return result;
}


}

4\ CustServiceImpl

package com.cust.service.impl;


import java.util.List;


import com.cust.dto.Customer;
import com.cust.mapper.CustDao;
import com.cust.service.CustService;


public class CustServiceImpl implements CustService {

private CustDao custDao;


public List<Customer> selectCustList() {
// TODO Auto-generated method stub
return custDao.selectCustList();
}


public Customer selectById(String id) {
// TODO Auto-generated method stub
return custDao.selectById(id);
}


public int delete(String id) {
// TODO Auto-generated method stub
return custDao.delete(id);
}


public int save(Customer cust) {
// TODO Auto-generated method stub
return custDao.save(cust);
}


public int update(Customer cust) {
// TODO Auto-generated method stub
return custDao.update(cust);
}


public CustDao getCustDao() {
return custDao;
}


public void setCustDao(CustDao custDao) {
this.custDao = custDao;
}


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于计算机专业的学生而言,参加各类比赛能够带来多方面的益处,具体包括但不限于以下几点: 技能提升: 参与比赛促使学生深入学习和掌握计算机领域的专业知识与技能,如编程语言、算法设计、软件工程、网络安全等。 比赛通常涉及实际问题的解决,有助于将理论知识应用于实践中,增强问题解决能力。 实践经验: 大多数比赛都要求参赛者设计并实现解决方案,这提供了宝贵的动手操作机会,有助于积累项目经验。 实践经验对于计算机专业的学生尤为重要,因为雇主往往更青睐有实际项目背景的候选人。 团队合作: 许多比赛鼓励团队协作,这有助于培养学生的团队精神、沟通技巧和领导能力。 团队合作还能促进学生之间的知识共享和思维碰撞,有助于形成更全面的解决方案。 职业发展: 获奖经历可以显著增强简历的吸引力,为求职或继续深造提供有力支持。 某些比赛可能直接与企业合作,提供实习、工作机会或奖学金,为学生的职业生涯打开更多门路。 网络拓展: 比赛是结识同行业人才的好机会,可以帮助学生建立行业联系,这对于未来的职业发展非常重要。 奖金与荣誉: 许多比赛提供奖金或奖品,这不仅能给予学生经济上的奖励,还能增强其成就感和自信心。 荣誉证书或奖状可以证明学生的成就,对个人品牌建设有积极作用。 创新与研究: 参加比赛可以激发学生的创新思维,推动科研项目的开展,有时甚至能促成学术论文的发表。 个人成长: 在准备和参加比赛的过程中,学生将面临压力与挑战,这有助于培养良好的心理素质和抗压能力。 自我挑战和克服困难的经历对个人成长有着深远的影响。 综上所述,参加计算机领域的比赛对于学生来说是一个全面发展的平台,不仅可以提升专业技能,还能增强团队协作、沟通、解决问题的能力,并为未来的职业生涯奠定坚实的基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值