相关jar包(注意替换掉特定数据库的jar包和特点tomcat的数据库连接池的jar包)
https://pan.baidu.com/s/1-FvtwAp_qfjtXxr10v7C0A
创建一个web工程springa,
把再识html+servlet 相关的代码复制到新的工程,包中间名称springa
添加jar包到项目
web.xml<!-- 配置核心控制器 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
框架配置文件spring-servlet.xml
Eclipse没有安装插件,直接复制,安装了就到航生成
idea生成。。。。。。。
spring-servlet.xml
WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>zpnhtml</display-name>
<welcome-file-list>
<welcome-file>zyy.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
View
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<title>Insert title here</title>
</head>
<body>
<div id="disUI" class="UI">
<a href="javascript:void(0)" id="toInsert" >增加</a>
<hr>
<input type="text" value="实现模糊查询">
<hr>
<table>
<thead>
<th>zyya</th>
<th>zyyb</th>
<th>zyyc</th>
<th>zyyd</th>
<th>操作</th>
</thead>
<tbody id="list">
</tbody>
</table>
</div>
<!--插入-->
<div id="updateUI" style="display: none" class="UI">
<table>
<tr>
<input type="hidden" id="oldzyya"/>
<td>zyya:<input type="text" id="zyya" />
<zsls id="zyyats" style="color: red;"></zsls>
</td>
</tr>
<tr>
<td>zyyb:<input type="text" id="zyyb"/></td>
</tr>
<tr>
<td>zyyc:<input type="text" id="zyyc" /></td>
</tr>
<tr>
<td>zyyd:<input type="text" id="zyyd" /></td>
</tr>
</table>
<input type="button" id="btnUpdate" value="insert">
<input type="button" class="back" value="back" >
</div>
<div id="loginUI" style="display: none" class="UI">
<p>user:<input type="text" name="logina" id="logina"></p>
<p>pass:<input type="password" name="loginb" id="loginb">
<font id="logints" color="red"></font>
</p>
<p><input type="button" id="btnLogin" value="登录" >
<input type="button" class="back" value="back" >
</p>
</div>
</body>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/zyy.js"></script>
</html>
$(function () {
$("#btnLogin").click(
() => { login(); }
);
$("#btnUpdate").click(
() => { update(); }
);
$("#toInsert").click(
() => { toUpdateUI("insert",{}); }
);
$(".back").click(
() => {
$(".UI").css("display", "none");
$("#disUI").css("display", "block");
}
);
getZyy();
})
function toUpdateUI(action,zyy) {
$.ajax({
url: "zyy/islogin.action",
success: function (res) {
if (res.indexOf("yes") != -1) {
$(".UI").css("display", "none");
$("#updateUI").css("display", "block");
$("#btnUpdate").val(action);
$("#zyya").val(zyy.zyya);
$("#oldzyya").val(zyy.zyya);
$("#zyyb").val(zyy.zyyb);
$("#zyyc").val(zyy.zyyc);
$("#zyyd").val(zyy.zyyd);
} else {
alert("not login");
$(".UI").css("display", "none");
$("#loginUI").css("display", "block");
}
}
});
}
function getZyy() {
$.ajax({
url: 'zyy/select.action',
dataType: "json",
success: function (data) {
var html = "";
for (var i = 0; i < data.length; i++) {
html += "<tr>";
html += "<td>" + data[i].zyya + "</td>";
html += "<td>" + data[i].zyyb + "</td>";
html += "<td>" + data[i].zyyc + "</td>";
html += "<td>" + data[i].zyyd + "</td>";
html += "<td><a href='javascript:void(0)' zyyid = '" + data[i].zyya + "' onclick='del(this)'>删除</a>";
html += " <a href='javascript:void(0)' onclick='toUpdateUI(\"update\"," + JSON.stringify(data[i]) + ")'> update </a></td>"
html += "</tr>";
}
$("#list").html(html);
}
})
}
// insert/update
function update() {
var action = $("#btnUpdate").val();
var url = "zyy/insert.action";
if (action == "update") {
url = "zyy/update.action";
}
console.log(url); //浏览器控制台查看日志
var oldzyya = $("#oldzyya").val();
var zyya = $("#zyya").val();
var zyyb = $("#zyyb").val();
var zyyc = $("#zyyc").val();
var zyyd = $("#zyyd").val();
$.ajax({
url: url,
data: { zyya: zyya, zyyb: zyyb, zyyc: zyyc, zyyd: zyyd, oldzyya: oldzyya },
success: function (res) {
if (res.indexOf("success")!=-1) {
$("#zyyats").html("请将增加或修改的数据显示到显示部分");
} else {
$("#zyyats").html(action+" failure");
}
},
error:function(res){
$("#zyyats").html(res.status);
}
});
}
/* 注意事项
var a=[1,2];
alert(a);
delete a[0];
alert(a);
*/
function del(id) {
if (confirm("del?")) {
$.ajax({
url: "zyy/delete.action?zyya=" + id,
success: function (res) {
alert(res);
}
});
}
}
function login() {
$.ajax({
url: "zyy/login.action",
type: "post",
data: {
zyya: $("#logina").val(),
zyyb: $("#loginb").val(),
},
success: function (res) {
if (res.indexOf("success") != -1) {
$(".UI").css("display", "none");
$("#disUI").css("display", "block");
} else {
$("#logints").html("invalid user or pass");
}
}
});
}
Controller
package com.springa.action;
import com.springa.model.Zyy;
import com.springa.service.ZyyService;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
@RequestMapping("/zyy")
public class ZyyController {
@Autowired
ZyyService zyyService;
@RequestMapping(value = "/login")
public void login(HttpServletRequest req, HttpServletResponse resp,Zyy zyy) throws ServletException, IOException {
PrintWriter out=resp.getWriter();
if (zyyService.check(zyy)) {
req.getSession().setAttribute("islogin","yes");
out.print("success");
} else {
req.getSession().setAttribute("islogin","no");
out.print("failure");
}
}
@RequestMapping("/insert")
public void insert(HttpServletRequest req, HttpServletResponse resp,Zyy zyy) throws ServletException, IOException {
PrintWriter out=resp.getWriter();
if(zyyService.insertZyy(zyy)) {
out.print("success");
}else {
out.print("failure");
}
}
@RequestMapping("/update")
public void update(HttpServletRequest req, HttpServletResponse resp,@RequestParam Map map) throws ServletException, IOException {
PrintWriter out=resp.getWriter();
if(zyyService.updateZyy(map)) {
out.print("success");
}else {
out.print("failure");
}
}
@RequestMapping("/delete")
public void delete(HttpServletRequest req, HttpServletResponse resp,int zyya) throws ServletException, IOException {
PrintWriter out=resp.getWriter();
if (zyyService.deleteZyy(zyya)) {
out.print("success!");
} else {
out.print("failure!");
}
}
@RequestMapping("/islogin")
public void islogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String isLogin=(String) req.getSession().getAttribute("islogin");
PrintWriter out=resp.getWriter();
if(isLogin==null) isLogin="no";
out.print(isLogin);
}
@RequestMapping("/check")
public void check(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int zyya = Integer.parseInt(req.getParameter("zyya"));
PrintWriter out=resp.getWriter();
if (zyyService.check(new Zyy(zyya))) {
out.print("exist");
} else {
out.print("none");
}
}
@RequestMapping("/select")
public void getZyy(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String zyyb = req.getParameter("zyyb");
if (zyyb == null) {
zyyb = "";
}
PrintWriter out=resp.getWriter();
out.print(zyyService.getZyy(new Zyy(zyyb)));
}
}
Service
package com.springa.service;
import com.springa.model.Zyy;
import java.util.List;
import java.util.Map;
public interface ZyyService {
List getZyy(Zyy zyy);
boolean insertZyy(Zyy zyy);
boolean deleteZyy(int lia);
boolean updateZyy(Map map);
boolean check(Zyy zyy);
}
package com.springa.service;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.springa.dao.ZyyDao;
import com.springa.mapper.ZyyMapper;
import com.springa.model.Zyy;
@Service
public class ZyyServiceImpl implements ZyyService {
@Autowired
ZyyDao dao;
@Override
public List getZyy(Zyy zyy) {
return dao.getData(ZyyMapper.class, zyy);
}
@Override
public boolean insertZyy(Zyy zyy) {
return dao.insert(ZyyMapper.class, zyy);
}
@Override
public boolean deleteZyy(int lia) {
return dao.delete(ZyyMapper.class, lia);
}
@Override
public boolean check(Zyy zyy) {
return dao.getObj(ZyyMapper.class, zyy);
}
@Override
public boolean updateZyy(Map map) {
return dao.update(ZyyMapper.class, map);
}
}
Model
package com.springa.model;
public class Zyy {
int zyya;
String zyyb;
double zyyc;
String zyyd;
public int getZyya() {
return zyya;
}
public void setZyya(int zyya) {
this.zyya = zyya;
}
public String getZyyb() {
return zyyb;
}
public void setZyyb(String zyyb) {
this.zyyb = zyyb;
}
public double getZyyc() {
return zyyc;
}
public void setZyyc(double zyyc) {
this.zyyc = zyyc;
}
public String getZyyd() {
return zyyd;
}
public void setZyyd(String zyyd) {
this.zyyd = zyyd;
}
public Zyy(int zyya) {
this.zyya = zyya;
}
public Zyy(String zyyb) {
this.zyyb = zyyb;
}
public Zyy(int zyya, String zyyb) {
this.zyya = zyya;
this.zyyb = zyyb;
}
@Override
public String toString() {
return "{\"zyya\":" + "\""+zyya +"\","
+"\"zyyb\":" + "\""+ zyyb +"\","
+"\"zyyc\":" + "\""+ zyyc +"\","
+"\"zyyd\":" + "\""+ zyyd +"\"}";
}
public Zyy(int zyya, String zyyb, double zyyc, String zyyd) {
this.zyya = zyya;
this.zyyb = zyyb;
this.zyyc = zyyc;
this.zyyd = zyyd;
}
public Zyy() {
}
}
package com.springa.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import com.springa.mapper.IMapper;
@Repository
public class ZyyDao {
@Autowired
SqlSessionFactory sqlSessionFactory;
private SqlSession getConn() {
SqlSession conn=null;
try {
// conn=MyBatisDAOUtil.getSqlSessionFactory().openSession();
conn=sqlSessionFactory.openSession();
} catch (Exception e) {
System.err.println(e.getMessage());
}
return conn;
}
public List getData(Class cls,Object obj) {
SqlSession conn=getConn();
try {
IMapper mapper=(IMapper)conn.getMapper(cls);
return mapper.getData(obj);
} finally {
conn.close();
}
}
public boolean insert(Class cls,Object obj) {
SqlSession conn=getConn();
int n=0;
try {
IMapper mapper=(IMapper)conn.getMapper(cls);
n=mapper.insert(obj);
conn.commit();
} finally {
conn.close();
}
return n>0;
}
public boolean delete(Class cls,int id) {
SqlSession conn=getConn();
int n=0;
try {
IMapper mapper=(IMapper)conn.getMapper(cls);
n=mapper.delete(id);
mapper.delete(id);
conn.commit();
} finally {
conn.close();
}
return n>0;
}
public boolean update(Class cls,Object obj) {
SqlSession conn=getConn();
int n=0;
try {
IMapper mapper=(IMapper)conn.getMapper(cls);
n=mapper.update(obj);
conn.commit();
} finally {
conn.close();
}
return n>0;
}
public boolean getObj(Class cls, Object obj) {
SqlSession conn=getConn();
int n=0;
try {
IMapper mapper=(IMapper)conn.getMapper(cls);
n=mapper.getObj(obj).size();
} finally{
conn.close();
}
return n>0;
}
}
package com.springa.mapper;
import java.util.List;
public interface IMapper {
List getData(Object obj);
int insert(Object obj);
int delete(int id);
int update(Object obj);
List getObj(Object obj);
}
package com.springa.mapper;
public interface ZyyMapper extends IMapper {
}
相关配置文件
<?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.springa.mapper.ZyyMapper">
<resultMap type="Zyy" id="ZyyResultSet">
<id property="zyya" column="zyya" />
<result property="zyyb" column="zyyb" />
<result property="zyyc" column="zyyc" />
<result property="zyyd" column="zyyd" />
</resultMap>
<select id="getData" resultMap="ZyyResultSet" resultType="Zyy">
SELECT * FROM zyy where zyyb like '%${zyyb}%'
</select>
<select id="getObj" parameterType="Zyy" resultType="Zyy">
SELECT * FROM zyy where zyya =#{zyya}
<if test="zyyb!=null">
and zyyb=#{zyyb}
</if>
</select>
<insert id="insert" parameterType="Zyy">
INSERT INTO zyy(zyya, zyyb, zyyc,zyyd)
VALUES(#{zyya}, #{zyyb}, #{zyyc}, #{zyyd})
</insert>
<update id="update" parameterType="Map">
UPDATE zyy SET zyya = #{zyya} , zyyb=#{zyyb},zyyc=#{zyyc},zyyd=#{zyyd}
WHERE zyya = #{oldzyya}
</update>
<delete id="delete" parameterType="int">
DELETE FROM zyy WHERE
zyya = #{zyya}
</delete>
</mapper>
<?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.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="com.springa"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/eat"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
<property name="initialSize" value="5"></property>
<property name="maxIdle" value="2"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml">
</property>
</bean>
</beans>
<?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>
<!-- <properties resource="com/springa/resources/db.properties" /> -->
<typeAliases>
<typeAlias type="com.springa.model.Zyy" alias="Zyy"></typeAlias>
</typeAliases>
<!-- <environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments> -->
<mappers>
<mapper resource="com/springa/mapper/ZyyMapper.xml" />
</mappers>
</configuration>
运行效果
首页
点击修改要登录
修改(500错误)
JAVASCRIP调试
增加
实现模糊查询