SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容)。常作为数据源较简单的web项目的框架。
1. Spring
new
一个对象,而是让Spring框架帮你来完成这一切。2. SpringMVC
3. mybatis
转载自博客:点击查看原文章
1. 持久层:DAO层(mapper)
DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,
2. 业务层:Service层
Service层主要负责业务模块的逻辑应用设计。
3. 表现层/控制层:Controller层(Handler层)
Controller层负责具体的业务模块流程的控制,在此层里面要调用Service层的接口来控制业务流程。
4. View层
此层与控制层结合比较紧密,需要二者结合起来协同工发。View层主要负责前台jsp页面的表示.
<?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_3_0.xsd" version="3.0">
<display-name>SSMdemo</display-name>
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>ssm</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ssm</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2.applicatonContext.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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!--mybatis关键: UserDao dao=session.getMapper(UserDao.class); -->
<!-- mybatis-spring(5,-1)第5个包最后一个类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 需要制定dao接口 setBasePackage() -->
<property name="basePackage" value="com.hqyj.dao"></property>
<!-- 需要SqlSession对象,由于框架比较智能,不用写ref注入,它也会自动给我们注入 -->
</bean>
<!-- 获取SqlSessionFactory. class值=jar包mybatis-spring(1,3)-->
<bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据库连接参数 -->
<property name="dataSource" ref="dbcp"></property>
<!-- 指定映射文件-->
<property name="mapperLocations" value="classpath:com/hqyj/sql/*.xml"></property>
</bean>
<!-- 数据库连接池 class值=jar包commons-dbcp(1,4) -->
<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="url" value="jdbc:mysql://localhost:3306/cloud_note?useUnicode=true&characterEncoding=utf8"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
<!-- 配置handlerMapping 可以用注解的方式代替 @RequestMapping -->
<!-- 想使用注解,需要先开启注解扫描 -->
<context:component-scan base-package="com.hqyj"></context:component-scan>
<!-- 开启驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".html"></property>
</bean>
</beans>
3.UserMapper.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命名空间,作用就是对sql进行分类化管理(sql隔离) -->
<!-- 辅配置文件 -->
<mapper namespace="com.hqyj.dao.UserDao">
//查询所有信息
<select id="findAll" resultType="com.hqyj.entity.User">
select * from cn_user; <!-- 注意:加不加分号都可以 -->
</select>
//按名字查找
<select id="findByName" resultType="com.hqyj.entity.User" parameterType="string">
select * from cn_user where cn_user_name like concat('%',#{name},'%') <!-- 注意:只有一个参数时,#{}内的内容可以随便写 -->
</select>
//增加数据
<!-- 由于是插入了所有信息,参数类型可以直接用对象即可,但是values值必须与对象的属性相同,不能随便写 -->
<insert id="save" parameterType="com.hqyj.entity.User">
insert into cn_user(cn_user_id,cn_user_name,cn_user_password,cn_user_token,cn_user_nick)
values(#{cn_user_id},#{cn_user_name},#{cn_user_password},#{cn_user_token},#{cn_user_nick})
</insert>
//删除数据
<delete id="delete" parameterType="string">
delete from cn_user where cn_user_name = #{user_name}
</delete>
//修改数据
<update id="update" parameterType="com.hqyj.entity.User">
update cn_user set cn_user_nick = #{cn_user_nick} where cn_user_name = #{cn_user_name}
</update>
</mapper>
4.UserDao.java(与UserMapper.xml的操作相对应)
package com.hqyj.dao;
import java.util.List;
import com.hqyj.entity.User;
public interface UserDao {
/*
* Mapper接口映射规则
1.id就是方法名
2.resultType就是返回值类型
3.ParameterType就是参数
4.namespace就是我们的接口路径:包名.接口名
*/
public List<User> findAll();
public List<User> findByName(String name);
public void save(User user);
public void delete(String name);
public void update(User user);
}
5.User.java
package com.hqyj.entity;
public class User {
private String cn_user_id;
private String cn_user_name;
private String cn_user_password;
private String cn_user_token;
private String cn_user_nick;
public String getCn_user_id() {
return cn_user_id;
}
public void setCn_user_id(String cn_user_id) {
this.cn_user_id = cn_user_id;
}
public String getCn_user_name() {
return cn_user_name;
}
public void setCn_user_name(String cn_user_name) {
this.cn_user_name = cn_user_name;
}
public String getCn_user_password() {
return cn_user_password;
}
public void setCn_user_password(String cn_user_password) {
this.cn_user_password = cn_user_password;
}
public String getCn_user_token() {
return cn_user_token;
}
public void setCn_user_token(String cn_user_token) {
this.cn_user_token = cn_user_token;
}
public String getCn_user_nick() {
return cn_user_nick;
}
public void setCn_user_nick(String cn_user_nick) {
this.cn_user_nick = cn_user_nick;
}
@Override
public String toString() {
return "User [cn_user_id=" + cn_user_id + ", cn_user_name=" + cn_user_name + ", cn_user_password="
+ cn_user_password + ", cn_user_token=" + cn_user_token + ", cn_user_nick=" + cn_user_nick + "]";
}
}
6.login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="ssm.do">
<input type="submit" value="SSM"></input>
</form>
</body>
</html>
7.UserController.java
package com.hqyj.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.hqyj.entity.User;
import com.hqyj.service.UserService;
//controller调用service
@Controller //这里用控制层组件注解,不用通用注解!相当于
public class UserController {
@Resource //相当于
private UserService service;
@RequestMapping("/ssm.do")
public String function(){
System.out.println(111);
List<User> users=service.testService();
for(User u:users){
System.out.println(u.getCn_user_name());
}
return "success";
}
}
8.UserService.java
package com.hqyj.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.hqyj.dao.UserDao;
//service调用dao
import com.hqyj.entity.User;
@Service
public class UserService {
@Resource
private UserDao userDao;
public List<User> testService(){
List<User> users=userDao.findByName("*d*");
return users;
}
}
9.success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
you have succeed!
</body>
</html>
10.测试