发表于: 2018-02-27 22:56:50
1 542
今天下午两点多到的深圳,做了一下student的项目的SSM,没想到经过一个假期,异乎寻常的顺利,犹如神助
1.主架构
2.pom.xml里面,配置不写了,有个*.xml要注意
</dependencies>
<build>
<finalName>web-ssm</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
3.StudentController
@Controller
//@RequestMapping("")
public class StudentController {
@Autowired
StudentService studentService;
@RequestMapping("listStudent")
public ModelAndView listStudent(Page page){
ModelAndView mav = new ModelAndView();
List<Student> ss= studentService.list(page);
int total = studentService.total();
page.caculateLast(total);
mav.addObject("ss", ss);
mav.setViewName("listStudent");
return mav;
}
@RequestMapping("addStudent")
public ModelAndView addStudent(Student student){
studentService.add(student);
ModelAndView mav = new ModelAndView("redirect:/listStudent");
return mav;
}
@RequestMapping("deleteStudent")
public ModelAndView deleteStudent(Student student){
studentService.delete(student);
ModelAndView mav = new ModelAndView("redirect:/listStudent");
return mav;
}
@RequestMapping("editStudent")
public ModelAndView editStudent(Student student){
Student s= studentService.get(student.getId());
ModelAndView mav = new ModelAndView("editStudent");
mav.addObject("s", s);
return mav;
}
@RequestMapping("updateStudent")
public ModelAndView updateStudent(Student student){
studentService.update(student);
ModelAndView mav = new ModelAndView("redirect:/listStudent");
return mav;
}
4.StudentMapper
public interface StudentMapper {
public int add(Student student);
public void delete(int id);
public Student get(int id);
public int update(Student student);
public List<Student> list();
public List<Student> list(Page page);public int total();
}
5.Student
public class Student {
private int id;
private String name;
private int qq;
private String major;
public Student() {
}
public Student(int id,String name,int qq,String major) {
super();
this.id = id;
this.name = name;
this.qq = qq;
this.major = major;
}
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 int getQq() {
return qq;
}
public void setQq(int qq) {
this.qq = qq;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", qq=" + qq +
", major='" + major + '\'' +
'}';
}
6.StudentServiceImpl
@Service
public class StudentServiceImpl implements StudentService{
@Autowired
StudentMapper studentMapper;
public List<Student> list(){
return studentMapper.list();
}
@Override
public List<Student> list(Page page) {
return studentMapper.list(page);
}
@Override
public int total() {
return studentMapper.total();
}
@Override
public void add(Student s) {
studentMapper.add(s);
}
@Override
public void update(Student s) {
studentMapper.update(s);
}
@Override
public void delete(Student s) {
studentMapper.delete(s.getId());
}
@Override
public Student get(int id) {
return studentMapper.get(id);
}
}
7.StudentService
public interface StudentService {
List<Student> list();
int total();
List<Student> list(Page page);
void add(Student s);
void update(Student s);
void delete(Student s);
Student get(int id);
}
8.Student.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.alibaba.mapper.StudentMapper">
<!--新增-->
<insert id="add" parameterType="com.alibaba.model.Student" >
insert into student ( id,name,qq,major ) values (#{id},#{name},#{qq},#{major})
</insert>
<!--修改-->
<update id="update" parameterType="com.alibaba.model.Student" >
update student set name=#{name},qq=#{qq},major=#{major} where id=#{id}
</update>
<!--删除-->
<delete id="delete" parameterType="int" >
delete from student where id= #{id}
</delete>
<select id="get" parameterType="_int" resultType="Student">
select * from student where id= #{id}
</select>
<select id="list" resultType="Student">
select * from student order by id desc
<if test="start!=null and count!=null">
limit #{start},#{count}
</if>
</select>
<select id="total" resultType="int">
select count(*) from student
</select>
</mapper>
9.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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<context:component-scan base-package="com.alibaba.service" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/mysql</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>1234</value>
</property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.alibaba.model" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/Student.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.alibaba.mapper"/>
</bean>
</beans>
10.springMVC.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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.alibaba.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<!-- 视图定位 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
11.editStudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div style="width:500px;margin:0px auto;text-align:center">
<div style="text-align:center;margin-top:40px">
<form method="post" action="updateStudent">
分类名称: <input name="name" value="${s.name}" type="text"> <br><br>
<input name="qq" value="${s.qq}" type="text"> <br><br>
<input name="major" value="${s.major}" type="text"> <br><br>
<input type="hidden" value="${s.id}" name="id">
<input type="submit" value="增加分类">
</form>
</div>
</div>
12.listStudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div style="width:500px;margin:0px auto;text-align:center">
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
<td>qq</td>
<td>major</td>
<td>编辑</td>
<td>删除</td>
</tr>
<c:forEach items="${ss}" var="s" varStatus="st">
<tr>
<td>${s.id}</td>
<td>${s.name}</td>
<td>${s.qq}</td>
<td>${s.major}</td>
<td><a href="editStudent?id=${s.id}">编辑</a></td>
<td><a href="deleteStudent?id=${s.id}">删除</a></td>
</tr>
</c:forEach>
</table>
<div style="text-align:center">
<a href="?start=0">首 页</a>
<a href="?start=${page.start-page.count}">上一页</a>
<a href="?start=${page.start+page.count}">下一页</a>
<a href="?start=${page.last}">末 页</a>
</div>
<div style="text-align:center;margin-top:40px">
<form method="post" action="addStudent">
分类名称: <input name="name" value="" type="text"> <br><br>
<input name="qq" value="" type="text"> <br><br>
<input name="major" value="" type="text"> <br><br>
<input type="submit" value="增加分类">
</form>
</div>
</div>
13.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"
xmlns:web="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_2_5.xsd" version="2.5">
<!-- 解决工程编码过滤器 -->
<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>
<!-- spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring mvc核心:分发servlet -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- spring mvc的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
14.index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
pageContext.setAttribute("path", request.getContextPath());
%>
<!DOCTYPE html >
<html>
<head>
<title>首页</title>
<style type="text/css">
a {
text-decoration: none;
color: #fff;
font-size: 14px;
}
h3 {
width: 180px;
height: 38px;
margin: 100px auto;
text-align: center;
line-height: 38px;
background: #5BC0DE;
border-radius: 4px;
}
</style>
</head>
<body>
<h3>
<a href="${path }listStudent">进入用户管理页</a>
</h3>
</body>
</html>
15MybatisTest 测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MybatisTest {
@Autowired
private StudentMapper studentMapper;
@Before
public void setUp() throws Exception {
}
@Test
public void testAdd() {
for (int i = 0; i < 5; i++) {
Student student = new Student();
student.setName("new Student");
studentMapper.add(student);
}
}
@Test
public void testTotal() {
int total = studentMapper.total();
System.out.println(total);
}
@Test
public void testList() {
Page p = new Page();
p.setStart(2);
p.setCount(3);
List<Student> ss=studentMapper.list(p);
for (Student s : ss) {
System.out.println(s.getName());
}
运行,查看结果,增删改查
点击编辑,跳转页面,如图。输入更改之后,也是可以的
明天的计划:提交任务二,开始任务三
遇到的问题:暂无
今天的收获:根据category项目完成了student项目的SSM
java任务二开始时间:2018.01.25
预计demo时间:2018.02.12
可能有延期风险,原因是:基础比较差,
禅道链接地址:http://task.ptteng.com/zentao/project-task-501.html
评论