bean
package cn.edu360.bean;
public class Student {
private int id;
private String name;
private String adress;
@Override
public String toString() {
return
"id=" + id +
", name='" + name + '\'' +
", adress=" + adress
+"\r\n";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public Student(int id, String name, String adress) {
this.id = id;
this.name = name;
this.adress = adress;
}
public Student() {
}
}
控制器
package cn.edu360.controller;
import cn.edu360.bean.Student;
import cn.edu360.mapper.StudentMapper;
import cn.edu360.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@org.springframework.stereotype.Controller
public class AllController {
@RequestMapping(value = {"/show"},method = RequestMethod.GET)
public ModelAndView showAll() throws Exception {
ModelAndView mv = new ModelAndView();
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactoryInstance();
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.showAll();
mv.addObject("message",list);
mv.setViewName("show");
return mv;
}
@RequestMapping(value="/deleteStudent")
public ModelAndView deleteById(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
SqlSessionFactory sqlSessionFactory =SqlSessionFactoryUtil.getSqlSessionFactoryInstance();
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
String s=httpServletRequest.getParameter("id");
int id=Integer.parseInt(s);
List<Student> list=mapper.showAll();
for (Student student:list) {
if(student.getId()==id){
mapper.deleteStudent(id);
session.commit();
mv.addObject("message","删除完成!");
mv.setViewName("delete");
return mv;
}
}
mv.addObject("message","该ID不存在,请核实!");
mv.setViewName("delete");
return mv;
}
@RequestMapping(value="/insertStudent")
public ModelAndView insertStudent(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactoryInstance();
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student=new Student();
String ids= httpServletRequest.getParameter("id");
String name=httpServletRequest.getParameter("name");
String adress=httpServletRequest.getParameter("adress");
int id=Integer.parseInt(ids);
student.setName(name);
student.setAdress(adress);
student.setId(id);
List<Student> list=mapper.showAll();
for (Student stu: list) {
if(stu.getId()==student.getId()){
mv.addObject("message","id已存在,添加失败,请核实后从新操作");
mv.setViewName("insert");
return mv;
}
}
mapper.insertStudent(student);
session.commit();
mv.addObject("message","恭喜你,添加成功!");
mv.setViewName("insert");
return mv;
}
@RequestMapping(value = {"/updateStudent"})
public ModelAndView updateStudent(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactoryInstance();
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student=new Student();
String ids= httpServletRequest.getParameter("id");
String name=httpServletRequest.getParameter("name");
String adress=httpServletRequest.getParameter("adress");
int id=Integer.parseInt(ids);
student.setName(name);
student.setAdress(adress);
student.setId(id);
List<Student> list=mapper.showAll();
for (Student stu: list) {
if(stu.getId()==student.getId()){
mapper.updateStudent(student);
session.commit();
mv.addObject("message","恭喜你,修改成功!");
mv.setViewName("update");
return mv;
}
}
mv.addObject("message","id不存在,修改失败,请核实后从新操作");
mv.setViewName("update");
return mv;
}
}
映射文件
package cn.edu360.mapper;
import cn.edu360.bean.Student;
import java.util.List;
public interface StudentMapper {
public List showAll();
public int insertStudent(Student student);
public int deleteStudent(int id);
public int updateStudent(Student studnet);
}
StudentMapper.xml
<resultMap id="studentResultMap" type="Student">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="adress" column="adress"></result>
</resultMap>
<!-- id 查询 -->
<select id="showAll" resultMap="studentResultMap">
select * from student
</select>
<insert id="insertStudent" parameterType="cn.edu360.bean.Student">
insert into student values(#{id},#{name},#{adress})
</insert>
<delete id="deleteStudent" parameterType="int">
delete from student where id=#{no}
</delete>
<update id="updateStudent" parameterType="cn.edu360.bean.Student">
update student set name=#{name},adress=#{adress} where id=#{id}
</update>
UTIL 通过单例模式管理SqlSessionFactory对象
package cn.edu360.util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.Reader;
public class SqlSessionFactoryUtil {
/*通过单例模式管理SqlSessionFactory对象/
private static SqlSessionFactory sqlSessionFactory = null;
public SqlSessionFactoryUtil() {
}
public synchronized static SqlSessionFactory getSqlSessionFactoryInstance() {
if (sqlSessionFactory != null) {
return sqlSessionFactory;
} else {
Reader reader;
try {
reader = Resources.getResourceAsReader("MybatisConfig.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
sqlSessionFactory = builder.build(reader);
} catch (IOException e) {
e.printStackTrace();
}
return sqlSessionFactory;
}
}
}
config.properties
#key=value
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/newsql?serverTimezone=Asia/Shanghai
username=root
password=123456
MybatisConfig.xml
<?xml version="1.0" encoding="UTF-8"?>SpringConfig.xml
<!-- ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 处理器 -->
<!--<bean name="/show" class="cn.edu360.s111.ShowController"></bean>-->
<!--<bean name="/insert" class="cn.edu360.s111.InsertStudentController"></bean>-->
<!--<bean name="/delete" class="cn.edu360.s111.DeleteStudentController"></bean>-->
<!--<bean name="/update" class="cn.edu360.s111.UpdateStudentController"></bean>-->
<bean name="/showAll" class="cn.edu360.controller.AllController"></bean>
DispatcherServlet-servlet.xml
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<context:component-scan base-package="cn.edu360" />
web.xml
Archetype Created Web Application
CharacterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 CharacterEncodingFilter /* DispatcherServlet org.springframework.web.servlet.DispatcherServlet 1 DispatcherServlet /delete.jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%>
<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>
请输入你要删除的学生信息的ID
<input type="submit" value="提交" />
index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<a href="/show" type="">查询所有</a>
<br>
<a href="/insertStudent.jsp">添加信息</a>
<br>
<a href="/deleteStudent.jsp">删除信息</a>
<br>
<a href="/updateStudent.jsp">修改信息</a>
insert.jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%>
<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>
i d |
姓名 |
地址 |
</form>
show.jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%>
<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>
</tr>
</c:forEach>
${stu} |
update.jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%>
<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>
updateStudent.jsp
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
请输入你要修改的学生信息的ID
请输入你要修改的学生姓名
请输入你要修改的学生地址
<input type="submit" value="提交" />
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
cn.edu360
MVC01
1.0-SNAPSHOT
war
MVC01 Maven Webapp
junit junit 4.11 test<!-- 配置servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- 配置jsp -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<!-- spring web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- mybatis整合spring时的插件 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!--使用AspectJ方式注解需要相应的包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<!--使用AspectJ方式注解需要相应的包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<!--引入Servlet开始-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!--引入Servlet结束-->
<!--引入jsp相关开始-->
<!--引入jsp相关结束--> <!--引入Javaee7开始-->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
org.apache.tomcat.maven tomcat7-maven-plugin
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>