这里面做的内容比较简单,功能的话,也就是增查删这三个功能,但是也是能够帮助大家学习到SSM的整合。
下面就介绍一下代码的部分。
1.首先要有这么多的jar包,反正我是这么多就可以了
2.然后就是你的xml配置,这三个框架中都有xml文件,其实都是将权限交给了spring来处理。spring就是将所有的需要new的,放到了IOC容器中,所以建立一个applicationContext.xml,这个是属于spring的xml。一个项目都是会有web.xml的。接下来配置Spring框架,本来applicationContext.xml中只需要配置bean就可以了,现在又将mybatis中的数据库相关-事务放到这里面,MyBatis的核心SqlSesseionFactory将其交给Dao层 。
这是数据库的信息,可以改成你自己的即可,这个是对应的下面 – 导入db.properties文件
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;databaseName =SpringThreeTierModel
username=sa
password=1228
maxactive=10
maxidle=6
对于这个小项目来说,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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context
http://www.springframework.org/schema/context/spring-context.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">
<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<!-- 导入db.properties文件 -->
<property name="locations">
<array>
<value>
classpath:db.properties
</value>
</array>
</property>
</bean>
<bean id="StudentServiceImplid" class="org.awen.serviceimpl.IStudentServiceImpl">
<!-- 适合StudentServiceImpl中的属性名是相同的。 -->
<property name="StudentDaoImpl" ref="StudentDaoImplid"></property>
</bean>
<!-- 第一种方式生成mapper对象 -->
<bean id="StudentDaoImplid" class="org.awen.daoimpl.IStudentDaoImpl">
<!-- 虽然Dao层里面没有任何属性,但是在其继承类中有这个属性
将spring配置的SQL Session Factory对象交给mapper(dao) -->
<property name="sqlSessionFactory" ref="SqlSessionFactory"></property>
</bean>
<!-- 配置数据库相关-事务
如果name里面报错那么久说明了 这个类中不包含这个属性 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="maxActive" value="${maxactive}"></property>
<property name="maxIdle" value="${maxidle}"></property>
</bean>
<!-- 在SPringIOC中 创建Mybatis得核心类SqlSessionFactory
MyBatis的核心SqlSesseionFactory将其交给Dao层 ,才可以使用-->
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 记载mapper.xml路径 org/awen/mapper/*.xml一次性将所有的xml文件添加到其中-->
<property name="mapperLocations" value="org/awen/dao/studentMapper.xml"></property>
</bean>
3.配置spingmvc.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<context:component-scan base-package="org.awen.handler"></context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置视图解析器的前缀和后缀 -->
<property name="prefix" value="/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
4.配置web.xml,在这里使用的是springmvc.xml的内容,因为使用的是Handler,也就是说控制器,不在是Servlet了
<?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>SpringMVCProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 设置xml的 路径 -->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 设置第一启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
5.接下来就是三层的东西了
5.1:
写的是Dao层的东西:在mybatis这中一般将其名字改为studentMapper.java,所以在这里也修改了,
并且在Dao层也写mybatis中studentMapper.xml的内容。
package org.awen.dao;
import java.util.List;
import org.awen.entity.Student;
public interface studentMapper {
//根据学号查询某一个学生的信息
Student QueryStudentBySno( int sno);
//查询全部学生
List<Student> QueryAllStudent() ;
//增加一个学生的信息 这里的 名字 一定要和 xml中 定义的相同
void AddOneStudent(Student student);
//按照学号删除一个学生的信息
void DeleteOneStudent(int sno);
}
5.2
将studenMapper.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">
<!-- namespace:该mapper.xml映射文件的 唯一标识 -->
<mapper namespace="org.awen.dao.studentMapper">
<!-- 后续通过namespace:id -->
<!-- resultType:查询返回结果值得类型,返回类型
这里需要说明一下不论数据库中的列名是什么 这里 的大小写 不会区分的
后面的参数是自己随便设置 与任何东西都没有关系-->
<select id="QueryStudentBySno" resultType="org.awen.entity.Student" parameterType="int">
select * from student where sno = #{sno}
</select>
<!-- 查询所有的学生信息 -->
<select id="QueryAllStudent" resultType="org.awen.entity.Student" parameterType="org.awen.entity.Student">
select * from student
</select>
<!-- 增加一个学生的信息 -->
<insert id="AddOneStudent" parameterType="org.awen.entity.Student">
insert into student(sno,sname,sage) values(#{sno},#{sname},#{sage})
</insert>
<!-- 删除一个学生的信息 -->
<delete id="DeleteOneStudent" parameterType="int">
delete from student where sno=#{sno}
</delete>
5.3
接下来就是Dao层的实现:
package org.awen.daoimpl;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.awen.dao.studentMapper;
import org.awen.entity.Student;
import org.mybatis.spring.support.SqlSessionDaoSupport;
public class IStudentDaoImpl extends SqlSessionDaoSupport implements studentMapper{
@Override
//查询某一个学生的信息
public Student QueryStudentBySno(int sno) {
// TODO 自动生成的方法存根
SqlSession session=super.getSqlSession();
studentMapper stuDao=session.getMapper(studentMapper.class);
Student student=stuDao.QueryStudentBySno(sno);
return student;
}
//查询所有学生的信息
public List<Student> QueryAllStudent() {
SqlSession session =super.getSqlSession();
studentMapper stuDao=session.getMapper(studentMapper.class);
//List<Student> students=new ArrayList<Student>();
List<Student> students =stuDao.QueryAllStudent();
//System.out.println(students);
return students;
}
//增加一个学生的信息
@Override
public void AddOneStudent(Student student) {
// TODO 自动生成的方法存根
SqlSession session =super.getSqlSession();
studentMapper stuDao=session.getMapper(studentMapper.class);
stuDao.AddOneStudent(student);
}
//按照学号删除某一个学生的信息
@Override
public void DeleteOneStudent(int sno) {
// TODO 自动生成的方法存根
SqlSession session =super.getSqlSession();
studentMapper stuDao=session.getMapper(studentMapper.class);
stuDao.DeleteOneStudent(sno);
}
}
5.4
其中还有学生实体类的写,遵循mybatis的规则,将表与数据库像对应起来。通俗的说就是数据库中的属性和表中的属性要保持一致。
package org.awen.entity;
public class Student {
private int sno;
private int sage;
private String sname;
public Student(int sno, int sage, String sname) {
super();
this.sno = sno;
this.sage = sage;
this.sname = sname;
}
public int getSno() {
return sno;
}
@Override
public String toString() {
return "Student [sno=" + sno + ", sage=" + sage + ", sname=" + sname + "]";
}
public void setSno(int sno) {
this.sno = sno;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
}
5.5
定义service的接口,和serviceIimpl,也就是对实现接口。
这个是service的接口
package org.awen.service;
import java.util.List;
import org.awen.entity.Student;
public interface IStudentService {
Student queryStudentBySno(int sno);
List<Student> queryAllStudent();
void DeleteOneStudent(int sno);
}
这个是serviceimpl的实现接口的
package org.awen.serviceimpl;
import java.util.List;
import org.awen.daoimpl.IStudentDaoImpl;
import org.awen.entity.Student;
import org.awen.service.IStudentService;
public class IStudentServiceImpl implements IStudentService{
IStudentDaoImpl StudentDaoImpl;
public void setStudentDaoImpl(IStudentDaoImpl studentDaoImpl) {
StudentDaoImpl = studentDaoImpl;
}
@Override
public Student queryStudentBySno(int sno) {
// TODO 自动生成的方法存根
Student student=StudentDaoImpl.QueryStudentBySno(sno);
return student;
}
public List<Student> queryAllStudent(){
List<Student> students=StudentDaoImpl.QueryAllStudent();
return students;
}
public void AddOneStudent(Student student) {
StudentDaoImpl.AddOneStudent(student);
}
public void DeleteOneStudent(int sno) {
StudentDaoImpl.DeleteOneStudent(sno);
}
}
6.最后就是书写SpringMVCHandler.java
package org.awen.handler;
import java.util.List;
import java.util.Map;
import org.awen.entity.Student;
import org.awen.serviceimpl.IStudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("handler")
public class SpringMVCHandler {
//@Autowired
@Qualifier(value="StudentServiceImplid")
IStudentServiceImpl StudentServiceImpl;
public void setStudentServiceImpl(IStudentServiceImpl studentServiceImpl) {
StudentServiceImpl = studentServiceImpl;
}
//这里查询的是某一个的学生信息:
@RequestMapping("welcome1")
public String welcome1(@RequestParam("sno") Integer sno ,Map<String,Object> map) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//当前得Servlet容器中拿其他容器中得东西
StudentServiceImpl=(IStudentServiceImpl)context.getBean("StudentServiceImplid");
Student student=StudentServiceImpl.queryStudentBySno(sno);
//System.out.println(student);
map.put("name", student.getSname());
map.put("sno", student.getSno());
map.put("age", student.getSage());
return "SomeOneStudent";
}
//这里查询的是全部学生的信息:
@RequestMapping("welcome2")
public String welcome2(Map<String,Object> map) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//当前得Servlet容器中拿其他容器中得东西
StudentServiceImpl=(IStudentServiceImpl)context.getBean("StudentServiceImplid");
List<Student> students=StudentServiceImpl.queryAllStudent();
//System.out.println(student);
map.put("students", students);
return "AllStudents";
}
//这里查询的是全部学生的信息:
@RequestMapping("welcome3")
public String welcome3(@RequestParam("sno") Integer sno ,Map<String,Object> map) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//当前得Servlet容器中拿其他容器中得东西
StudentServiceImpl=(IStudentServiceImpl)context.getBean("StudentServiceImplid");
StudentServiceImpl.DeleteOneStudent(sno);
List<Student> students=StudentServiceImpl.queryAllStudent();
map.put("students", students);
return "AllStudents";
}
//这里是按照学号删除学生的信息
@RequestMapping("welcome4")
public String welcome4(@RequestParam("sno") Integer sno ,Map<String,Object> map) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//当前得Servlet容器中拿其他容器中得东西
StudentServiceImpl=(IStudentServiceImpl)context.getBean("StudentServiceImplid");
StudentServiceImpl.DeleteOneStudent(sno);
List<Student> students=StudentServiceImpl.queryAllStudent();
map.put("students", students);
return "AllStudents";
}
}
7.在最后就是 一些显示 的jsp,也就是界面的信息了,自己也可以将其美化一下。
在这里需要注意看清楚它们的文件路径
强调需要注意jsp的路径,这就是全部的路径配置。结合这上面的代码进行,补充就可以了。下面就是将各个jsp的代码进行补充。
1.首先是addStudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="handler/welcome3">
学号:<input type="text" name="sno"><br>
姓名:<input type="text" name="sname"><br>
年龄:<input type="text" name="sage"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
2.index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="QueryStudent.jsp">1.查询某一个学生</a>
<a href="handler/welcome2">2.查询所有的学生</a>
<a href="addStudent.jsp">3.增加一学生信息</a>
</body>
</html>
3.QueryStudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="handler/welcome1">
学号:<input type="text" name="sno"><br>
<input type="submit" value="查询">
</form>
</body>
</html>
4.AllStudents.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.List"%>
<%@page import="org.awen.entity.Student" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1px">
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>删除</th>
</tr>
<%
List<Student> students=(List<Student>)request.getAttribute("students");
for(Student student:students){
%>
<tr>
<td><%=student.getSno()%></td>
<td><%=student.getSname()%></td>
<td><%=student.getSage()%></td>
<td><a href="welcome4?sno=<%=student.getSno()%>">删除</a></td>
<br>
</tr>
<%
}
%>
</table>
</body>
</html>
5.SomeOneStudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 在这里是查询某一个学生的信息 查询的结果 为 这些东西:首先做一个表格将其写在里面 -->
<table border="1px">
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<tr>
<td>${requestScope.sno}</td>
<td>${requestScope.name}</td>
<td>${requestScope.age}</td>
</tr>
</table>
</body>
</html>
差不多也就这么些了,其余的按照自己的补充也是可以将其完整话的。有什么不懂的话联系
公众号:里面也有很多 的教程和资料以及工具。