ssm+maven
spring、springmvc、mybatis整合。
1.配置maven版本
1.1maven下载
地址:http://maven.apache.org/download.cgi
1.2配置中央库
目前最流行的中央库是阿里云。
https://maven.aliyun.com/nexus/content/groups/public/
步骤:配置本地maven
①打开maven中conf文件夹,打开setting.xml文件:
②找到mirrors节点,添加如下内容:
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>alien maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
1.3配置eclipse
配置eclipse的maven:
①installations
②User Settings
步骤: eclipse新建maven项目
①Group id是公司名,org.lee
②Artifact id是项目名,mavenDemo
2.pom.xml中导jar
spring-context
spring-beans
spring-core
spring-web
spring-aop
spring-jdbc
spring-tx
spring-webmvc
mybatis
mybatis-spring
jstl
mysql-connector-java
aspectjweaver
log4j
slf4j-api
dpcp
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.lee</groupId>
<artifactId>ssmdemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ssmdemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!-- aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- commons-dbcp -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
<build>
<finalName>ssmdemo</finalName>
</build>
</project>
3.配置文件
①web.xml
1.字符编码过滤器
2.springMVC控制器
3.spring控制器
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ssmdemo</display-name>
<!-- 1.字符编码过滤器 -->
<filter>
<filter-name>encodingFilter</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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 2.springMVC核心控制器 -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 3.spring控制器 -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
</web-app>
②spring-mvc.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: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="org.lee.ssmdemo.controller"/>
<!-- 静态资源可访问的设置方式 -->
<mvc:default-servlet-handler />
<!-- 注册MVC注解驱动 -->
<mvc:annotation-driven />
<!-- 视图解析器,若不设置会一句springMVC默认设置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
③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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!-- 开启包扫描 -->
<context:component-scan base-package="org.lee.ssmdemo"></context:component-scan>
<!-- DBCP连接池 -->
<context:property-placeholder location="classpath:dbconfig.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="url" value="${jdbc.jdbcUrl}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 注册mybatis的selsessionfactory的bean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:org/lee/ssmdemo/mapper/*.xml"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- mapper接口的包 -->
<property name="basePackage" value="org.lee.ssmdemo.mapper" ></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
④dbconfig.properties
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/demo
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.user=root
jdbc.password=root
⑤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>
<!-- 开启日志 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
<!-- 开启二级缓存 -->
<!-- <setting name="cacheEnabled" value="true"/> -->
</settings>
<!-- 设置别名 -->
<typeAliases>
<package name="org.lee.ssmdemo.entity"/>
</typeAliases>
</configuration>
⑥log4j.properties
log4j.rootLogger = DEBUG,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =%d %-5p %m %n
4.建表、实体类
①学生表
CREATE TABLE `student` (
`stuid` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`stucard` int(11) DEFAULT NULL,
PRIMARY KEY (`stuid`),
KEY `fk_student_card_id` (`stucard`),
CONSTRAINT `fk_student_card_id` FOREIGN KEY (`stucard`) REFERENCES `studentcard` (`cardid`)
)
②学生证表
CREATE TABLE `studentcard` (
`cardid` int(11) NOT NULL AUTO_INCREMENT,
`stuname` varchar(20) DEFAULT NULL,
PRIMARY KEY (`cardid`)
)
③学生类
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private Integer stuid;
private String name;
private Date birthday;
private StudentCard stucard;
//getter,setter略
}
④学生证类
public class StudentCard implements Serializable {
private static final long serialVersionUID = 1L;
private Integer cardId;
private String stuName;
//getter,setter略
}
5.写mapper的接口和mapper的配置文件
通常写在同一个包下。
四同:
接口名 = 配置文件名
接口的方法名 = 配置文件的id
接口的返回值类型 = 配置文件的resultType
接口的参数值类型 = 配置文件的parameterType
①StudentMapper.java
@Repository("studentMapper")
public interface StudentMapper {
List<Student> getAllStudents();
int remoStudent(int stuid);
}
②StudentMapper.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 namespace="org.lee.ssmdemo.mapper.StudentMapper">
<resultMap type="student" id="studentResult">
<id property="stuid" column="stuid"/>
<result property="name" column="name"/>
<result property="birthday" column="birthday"/>
<association property="stucard" javaType="StudentCard">
<id property="cardId" column="cardid"/>
<result property="stuName" column="stuname"/>
</association>
</resultMap>
<select id="getAllStudents" resultMap="studentResult">
select * from (select a.* ,b.* from student a inner join studentcard b on a.stucard = b.cardid) as n
</select>
<delete id="remoStudent" parameterType="int">
delete from student where stuid=#{stuid}
</delete>
</mapper>
6.写业务层
①StudentService.java
public interface StudentService {
List<Student> getAllStudents();
boolean remoStudent(int stuid);
}
②StudentServiceImpl.java
@Service("studentService")
public class StudentServiceImpl implements StudentService {
private StudentMapper studentMapper;
@Resource(name="studentMapper")
public void setStudentMapper(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
@Override
public List<Student> getAllStudents() {
List<Student> students = studentMapper.getAllStudents();
return students;
}
@Override
public boolean remoStudent(int stuid) {
int i = studentMapper.remoStudent(stuid);
if (i==0) {
return false;
}
return true;
}
}
7.写控制器
@Controller
public class StudentController {
private StudentService studentService;
@Resource(name="studentService")
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
@RequestMapping("/showStudent")
public ModelAndView showStudents() {
ModelAndView mv = null;
try {
mv = new ModelAndView("show");
List<Student> students = studentService.getAllStudents();
mv.addObject("data",students);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mv;
}
@RequestMapping("/removeStudent")
public ModelAndView removeStudent(int stuid) {
ModelAndView mv = new ModelAndView("error");
boolean remoStudent = studentService.remoStudent(stuid);
if (remoStudent) {
return showStudents();
}else {
mv.addObject("msg","删除不成功!");
return mv;
}
}
}
8.写前端页面
①show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<th>stuid</th>
<th>name</th>
<th>birthday</th>
<th>cardid</th>
<th>stuname</th>
<th>删除</th>
</tr>
<c:forEach var="row" items="${data }">
<tr>
<td>${row.stuid }</td>
<td>${row.name }</td>
<td><fmt:formatDate value="${row.birthday }" pattern="yyyy-MM-dd"/></td>
<td>${row.stucard.cardId }</td>
<td>${row.stucard.stuName }</td>
<td><a href="javascript:removeStudent(${row.stuid })">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
<script type="text/javascript">
function removeStudent(id) {
if(confirm("确定删除?")){
window.location= "removeStudent.action?stuid="+id;
}
}
</script>
</html>
②error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>${msg }</p>
</body>
</html>