SI框架实现增删查改


SimpleController

package com.demo.controllers;


import java.util.List;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import com.demo.database.data.TDemoCompany;
import com.demo.services.ISimpleService;


/**
 * 业务控制器
 * 测试si框架
 * @author Teacher
 * @createTime 2018年4月4日 下午6:01:39
 * @updateTime 2018年4月4日 下午6:01:39
 * @version 1.0.0
 */
@Controller
@RequestMapping("/simple/")
public class SimpleController {


@Resource
private ISimpleService isimpleService;

@RequestMapping("query.htm")
public String query(Model model) {
try {
List<TDemoCompany> list = isimpleService.query();
model.addAttribute("list", list);
System.out.println(list);
return "simple";
} catch (Exception e) {
e.printStackTrace();
}
return "error";
}

@RequestMapping("add.htm")
public String add(HttpServletRequest request){
try {
String id=request.getParameter("id");
String companyName=request.getParameter("companyName");
TDemoCompany tdc = new TDemoCompany(id,companyName);
isimpleService.add(tdc);
return "redirect:/simple/query.htm";
} catch (Exception e) {
e.printStackTrace();
}
return "error";
}

@RequestMapping("delete.htm")
public String delete(@Param("id")String id){
try {
//System.out.println(id);
isimpleService.delete(id);
return "redirect:/simple/query.htm"; 
} catch (Exception e) {
e.printStackTrace();
}
return "error";
}

@RequestMapping("queryById.htm")
public String queryById(@Param("id")String id,HttpServletRequest request){
try {
TDemoCompany comp = isimpleService.queryById(id);
System.out.println(id);
request.setAttribute("comp",comp);
return "cimpanyview"; 
} catch (Exception e) {
e.printStackTrace();
}
return "error";
}

@RequestMapping("update.htm")
public String update(@Param("id")String compId,HttpServletRequest request){
try {
compId=request.getParameter("id");
String compName=request.getParameter("companyName");
TDemoCompany comp = new TDemoCompany(compId, compName);
isimpleService.update(comp);
return "redirect:/simple/query.htm";
} catch (Exception e) {
e.printStackTrace();
}
return "error";
}

}

TDemoCompanyMapper

package com.demo.database.dao;


import java.util.List;


import com.demo.database.data.TDemoCompany;


/**
 * 公司数据操作的映射接口
 * @author Teacher
 * @createTime 2018年4月4日 下午5:50:46
 * @updateTime 2018年4月4日 下午5:50:46
 * @version 1.0.0
 */
public interface TDemoCompanyMapper {


public List<TDemoCompany> query() throws Exception;

public void add(TDemoCompany comp) throws Exception;

public void delete(String id) throws Exception;

public TDemoCompany queryById(String id) throws Exception;

public void update(TDemoCompany comp) throws Exception;

}

TDemoCompany

package com.demo.database.data;


import java.io.Serializable;


public class TDemoCompany implements Serializable {


private String compId;
private String compName;


public String getCompId() {
return compId;
}


public void setCompId(String compId) {
this.compId = compId;
}


public String getCompName() {
return compName;
}


public void setCompName(String compName) {
this.compName = compName;
}


public TDemoCompany(String compId, String compName) {
super();
this.compId = compId;
this.compName = compName;
}



}

TDemoCompany.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.demo.database.dao.TDemoCompanyMapper">


    <!-- 定义返回结果集 -->
    <resultMap id="companyResultSet" type="TDemoCompany">
        <id property="compId" column="comp_id" />
        <result property="compName" column="comp_name" />
    </resultMap>
    
    <!-- 查询 -->
    <select id="query" resultMap="companyResultSet">
        select * from t_demo_company
    </select>
    
    <!-- 添加 -->
    <insert id="add" parameterType="TDemoCompany">
        insert into t_demo_company(comp_id,comp_name) values(#{compId},#{compName})
    </insert>
    
    <delete id="delete">
        delete from t_demo_company where comp_id =#{id}
        <!-- in
        <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
            #{item}
        </foreach> -->
    </delete>
    
     <select id="queryById" resultMap="companyResultSet">
        select * from t_demo_company where comp_id =#{id}
    </select> 
    
    <update id="update" parameterType="TDemoCompany">
    update t_demo_company set comp_name=#{compName} where comp_id=#{compId}
    </update>


</mapper>

ISimpleService

package com.demo.services;


import java.util.List;


import com.demo.database.data.TDemoCompany;


public interface ISimpleService {


public List<TDemoCompany> query() throws Exception;

public void add(TDemoCompany comp) throws Exception;

public void delete(String id) throws Exception;

public TDemoCompany queryById(String id) throws Exception;

public void update(TDemoCompany comp) throws Exception;


}

SimpleServiceImpl

package com.demo.services.impl;


import java.util.List;


import javax.annotation.Resource;


import org.springframework.stereotype.Service;


import com.demo.database.dao.TDemoCompanyMapper;
import com.demo.database.data.TDemoCompany;
import com.demo.services.ISimpleService;


@Service
public class SimpleServiceImpl implements ISimpleService {


@Resource
private TDemoCompanyMapper mapper;

@Override
public List<TDemoCompany> query() throws Exception {
List<TDemoCompany> list = mapper.query();
return list;
}


@Override
public void add(TDemoCompany comp) throws Exception {
mapper.add(comp);

}


@Override
public void delete(String id) throws Exception {
mapper.delete(id);
}


@Override
public TDemoCompany queryById(String id) throws Exception {


return mapper.queryById(id);

}


@Override
public void update(TDemoCompany comp) throws Exception {
mapper.update(comp);
}


}


applicationContext.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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">


<!-- 定义数据源Bean -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/demo"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 定义会话工厂Bean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 定义映射扫描配置Bean -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="com.demo.database.dao" />
</bean>

</beans>


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>

    <!-- 设置别名 -->
<typeAliases>
     <typeAlias alias="TDemoCompany" type="com.demo.database.data.TDemoCompany"/>
</typeAliases>

<!-- 设置映射文件 -->
<mappers>
<mapper resource="com/demo/database/xml/TDemoCompany.xml" />
</mappers>

</configuration>


spring-mvc

<?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.xsd
        http://www.springframework.org/schema/aop  
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
          
    <!-- 启用注解 -->
    <context:component-scan base-package="com.demo" /> 
        
    <!-- 定义视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </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" 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>demo_si_test</display-name>
  
  <!-- 定义前端控制器 -->
  <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:spring-*.xml,classpath:applicationContext*.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>SpringMVC</servlet-name>
  <url-pattern>*.htm</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>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>characterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
  

</web-app>


add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%
String base = request.getScheme() + "://" + request.getServerName() + ":" 
+ request.getServerPort() + request.getContextPath() + "/";
%>
<base href="<%=base%>"> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="simple/add.htm" method="post" >
<p>公司Id:<input type="text" name="id"></p>
<p>公司名称:<input type="text" name="companyName"></p>
<p><input type="submit" value="添加"></p>
</form>
</body>

</html>


cimpanyview.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<%
String base = request.getScheme() + "://" + request.getServerName() + ":" 
+ request.getServerPort() + request.getContextPath() + "/";
%>
<base href="<%=base%>"> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="simple/update.htm" method="post" >
<p>公司Id:<input type="text" name="id" value="${comp.compId}" readonly="readonly"></p>
<p>公司名称:<input type="text" name="companyName" value="${comp.compName}"></p>
<p><input type="submit" value="修改"></p>
</form>
</body>

</html>


simple.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<%
String base = request.getScheme() + "://" + request.getServerName() + ":" 
     + request.getServerPort() + request.getContextPath() + "/";
%>
<base href="<%=base%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
   <h2>公司信息列表</h2>
   <hr/>
   <table  width="900" align="center" cellpadding="5" cellspacing="0"
border="1">
    <tr>  
        <th>companyId</th>  
        <th>companyName</th>  
        <th>Operate</th>  
        </tr>  
   <c:forEach var="comp" items="${requestScope.list }" varStatus="st">
    <tr>  
                <td>${comp.compId }</td>  
                <td>${comp.compName }</td>  
                <td>  
                    <a href="/demo_si_test/simple/queryById.htm?id=${comp.compId }">查看</a>  
                    <a href="/demo_si_test/simple/delete.htm?id=${comp.compId }" >删除</a>  
                    <a href="/demo_si_test/simple/queryById.htm?id=${comp.compId }" >修改</a>  
                </td>  
      </tr>
   </c:forEach>
   </table>
   <p align="center"><a href="add.jsp" >添加</a></p>
</body>
</html>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值