7月15日 SSM 周日

单表增删改查

<?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">
    <!-- springmvc -->
    <servlet>
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <!-- 过滤器 -->
    <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>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- spring上下文 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>


  <display-name></display-name> 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
<?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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <!-- 组件扫描 -->
    <context:component-scan base-package="com.controller"/>
    <!-- 开启MVC驱动 -->
    <mvc:annotation-driven/>
    <!-- 视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
<?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:jee="http://www.springframework.org/schema/jee"  
xmlns:lang="http://www.springframework.org/schema/lang"  
xmlns:util="http://www.springframework.org/schema/util"  
xmlns:tx="http://www.springframework.org/schema/tx"  
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/aop    
 http://www.springframework.org/schema/aop/spring-aop.xsd   
 http://www.springframework.org/schema/jee    
 http://www.springframework.org/schema/jee/spring-jee.xsd   
 http://www.springframework.org/schema/lang    
 http://www.springframework.org/schema/lang/spring-lang.xsd   
 http://www.springframework.org/schema/context    
 http://www.springframework.org/schema/context/spring-context.xsd   
 http://www.springframework.org/schema/tx    
 http://www.springframework.org/schema/tx/spring-tx.xsd   
 http://www.springframework.org/schema/util    
 http://www.springframework.org/schema/util/spring-util.xsd   
 http://www.springframework.org/schema/mvc    
 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <!-- 组件扫描 -->
    <context:component-scan base-package="com"></context:component-scan>
    <!-- 创建数据池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql:///ssm01"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <!-- 创建sqlsessionfactory -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath:com/mapper/*.xml"></property>
    </bean>
    <!-- 扫描mybatis操作数据库的接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mapper"></property>
    </bean>
 </beans>
package com.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dto.Dog;
import com.service.DogService;

@Controller
public class DogController {
    @Autowired
    private DogService dogService;

    @RequestMapping("list")
    public String list(HttpServletRequest request){
        System.out.println("list");
        List<Dog> dogList = dogService.findDogList();
        System.out.println(dogList);
        request.setAttribute("dogList", dogList);
        return "list";
    }
    @RequestMapping("add")
    public void add(Dog dog,HttpServletResponse response) throws IOException{
        int i = dogService.add(dog);
        System.out.println(i);
        response.getWriter().print(i);
    }
    @RequestMapping("del")
    public void del(Dog dog,HttpServletResponse response) throws IOException{
        int i = dogService.del(dog);
        response.getWriter().print(i);
    }
    @RequestMapping("show")
    @ResponseBody
    public Map show(Dog dog){
        Dog d = dogService.getObj(dog);
        System.out.println(d);
        Map<String, Dog> map = new HashMap<String, Dog>();
        map.put("d", d);
        return map;
    }
    @RequestMapping("update")
    public void update(Dog dog,HttpServletResponse response) throws IOException{
        int i = dogService.update(dog);
        System.out.println(i);
        response.getWriter().print(i);
    }
}
package com.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dto.Dog;
import com.mapper.DogMapper;

@Service
@Transactional
public class DogService {

    @Autowired
    private DogMapper dogMapper;

    public List<Dog> findDogList() {
        return dogMapper.findDogList();
    }

    public int add(Dog dog) {
        return dogMapper.add(dog);
    }

    public int del(Dog dog) {
        return dogMapper.del(dog);
    }

    public Dog getObj(Dog dog) {
        return dogMapper.getObj(dog);
    }

    public int update(Dog dog) {
        return dogMapper.update(dog);
    }

}
package com.mapper;

import java.util.List;

import com.dto.Dog;

public interface DogMapper {

    List<Dog> findDogList();

    int add(Dog dog);

    int del(Dog dog);

    Dog getObj(Dog dog);

    int update(Dog dog);

}
<?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.mapper.DogMapper">

    <sql id="all">id,name,sex,age</sql>

    <select id="findDogList" resultType="com.dto.Dog">
        select <include refid="all"></include> from t_dog
    </select>

    <insert id="add">
        insert into t_dog set id=${id},name=#{name},sex=#{sex},age=${age}
    </insert>

    <delete id="del">
        delete from t_dog where id=${id}
    </delete>

    <select id="getObj" resultType="com.dto.Dog">
        select <include refid="all"></include> from t_dog where id=${id}
    </select>

    <update id="update">
        update t_dog set name=#{name},sex=#{sex},age=${age} where id=${id}
    </update>
</mapper>
package com.dto;

public class Dog {
    private Integer id;
    private String name;
    private String sex;
    private Integer age;
    public Dog() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Dog(Integer id, String name, String sex, Integer age) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Dog [id=" + id + ", name=" + name + ", sex=" + sex + ", age="
                + age + "]";
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }


}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
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>My JSP 'list.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  <script type="text/javascript" src="js/jquery-1.8.2.js"></script>
    <script type="text/javascript">
        function del(id){
            if(confirm("确认要删除吗?")){
                $.post(
                    "del.do",
                    {id:id},
                    function (data){
                        if(data==1){
                            alert("删除成功");
                            location="list.do";
                        }else{
                            alert("删除失败");
                        }
                    }
                );
            }
        }
    </script>
  </head>

  <body>
    <table>
        <tr>
            <th>id</th>
            <th>name</th>
            <th>sex</th>
            <th>age</th>
            <th><input type="button" value="添加" onclick="location='add.jsp'"></th>
        </tr>
        <c:forEach var="d" items="${dogList}">
            <tr>
                <th>${d.id }</th>
                <th>${d.name }</th>
                <th>${d.sex }</th>
                <th>${d.age }</th>
                <th><input type="button" value="删除" onclick="del(${d.id})">
                <input type="button" value="修改" onclick="location='update.jsp?id=${d.id}'"></th>
            </tr>
        </c:forEach>
    </table>
  </body>
</html>
<%@ 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>My JSP 'add.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  <script type="text/javascript" src="js/jquery-1.8.2.js"></script>
    <script type="text/javascript">
        function sub(){
            var id = $("[name='id']").val();
            var name = $("[name='name']").val();
            var sex = $("[name='sex']").val();
            var age = $("[name='age']").val();

            $.post(
                "add.do",
                {id:id,name:name,sex:sex,age:age},
                function (data){
                    if(data==1){
                        alert("添加成功");
                        location="list.do";
                    }else{
                        alert("添加失败");
                    }
                },"json"
            );
        }
    </script>

  </head>

  <body>
    id:<input name="id"><br>
    name:<input name="name"><br>
    sex:<input name="sex"><br>
    age:<input name="age"><br>
    <input type="button" value="添加" onclick="sub()">
  </body>
</html>
<%@ 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>My JSP 'update.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  <script type="text/javascript" src="js/jquery-1.8.2.js"></script>
    <script type="text/javascript">
        var id = "${param.id}";
        $.post(
            "show.do",
            {id:id},
            function (data){
                var dog = data.d;
                $("[name='id']").val(dog.id);
                $("[name='name']").val(dog.name);
                $("[name='sex']").val(dog.sex);
                $("[name='age']").val(dog.age);
            },"json"
        );

        function sub(){
            $.post(
                "update.do",
                $("form").serialize(),
                function (data){
                    if(data==1){
                        alert("修改成功");
                        location="list.do";
                    }else{
                        alert("修改失败");
                    }
                },"json"
            );
        }
    </script>
  </head>

  <body>
  <form>
    id:<input name="id" type="hidden"><br>
    name:<input name="name"><br>
    sex:<input name="sex"><br>
    age:<input name="age"><br>
  </form>
    <input type="button" value="修改" onclick="sub()">
  </body>
</html>

“每一个人在本性上都想求知。
《形而上学》——亚里士多德

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值