建表
快速建一个userinfos表,并插入两条数据
show databases;
drop database if exists mydemo;
create database mydemo;
use mydemo;
create table userinfos(
userid int primary key not null auto_increment,
username varchar(20) not null,
birthday date not null);
show tables;
insert into userinfos(username,birthday) values ('aaa','1996-9-9');
insert into userinfos(username,birthday) values('bbb','1996-9-9');
select * from userinfos;
新建项目,引入jar包
项目结构预览
由于40分钟没能搓完,这会儿已经ssmdemo6了!,想要框架玩的6,基础功必须扎实!!
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kgc</groupId>
<artifactId>ssmdemo2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>ssmdemo2 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring-version>4.3.7.RELEASE</spring-version>
<json-version>2.9.8</json-version>
</properties>
<!-- 单元测试 -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 2.数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- DAO: MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 3.Servlet web -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<!--provided表明该包只在编译和测试的时候用,因为tomcat也有servlet-api包,
此处为了防止冲突-->
<scope>provided</scope>
</dependency>
<!--jsp标签库-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Json-->
<!--序列化,将对象转换成json-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${json-version}</version>
</dependency>
<!--反序列化-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${json-version}</version>
</dependency>
<!--注释语法 使json按按要求返回信息-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${json-version}</version>
</dependency>
<!-- 4.Spring -->
<!-- 1)Spring核心 -->
<!--核心容器,提供基本框架-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<!--负责实现Spring框架的IOC模块-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-version}</version>
</dependency>
<!--spring应用上下文-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- 2)Spring DAO层 -->
<!--分离业务逻辑和数据库交互代码-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<!--实现事务管理-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- 3)Spring web -->
<!--分离事物逻辑视图控制器-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
</dependencies>
<build>
<finalName>ssmdemo2</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<!--解决maven国内慢-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<!--mapper产生对应的xml文件-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
src.main.java
entity 实体类
package com.kgc.ssmdemo2.entity;
import java.io.Serializable;
import java.util.Date;
/**
* @author:J
* @date:2019/11/13
* @aim:
*/
public class Userinfos implements Serializable {
private int userid;
private String username;
private Date birthday;
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
dao 接口
package com.kgc.ssmdemo5.dao;
import com.kgc.ssmdemo5.entity.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface UserDAO {
@Insert("insert into userinfos(username,birthday) values (#{username},#{birthday}) ")
public void insertUser(User user);
@Delete("delete from userinfos where userid=#{userid}")
public void delUser(@Param("userid") int userid);
@Update("update userinfos set username=#{username},birthday=#{birthday} where userid=#{userid}")
public void modUser(User user);
@Select("select * from userinfos")
public List<User> findAll();
@Select("select * from userinfos where userid=#{userid}")
public User findById(@Param("userid") int userid);
}
这里用了几个注解的小操作,省去了后面写usermapper.xml
和mybatis-config.xml
,距离40分钟搓完的小目标又近了一步,哈哈!
service
package com.kgc.ssmdemo2.service;
import com.kgc.ssmdemo2.dao.UserDAO;
import com.kgc.ssmdemo2.entity.Userinfos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* @author:J
* @date:2019/11/13
* @aim:
*/
@Service
@Transactional
public class UserService {
@Autowired
private UserDAO userDAO;
public void saveUser(Userinfos user){
userDAO.insertUser(user);
}
public void deUser(int userid){
userDAO.delUser(userid);
}
public void modUser(Userinfos user){
userDAO.modUser(user);
}
public List<Userinfos> findAll(){
return userDAO.findAll();
}
public Userinfos finByid(int userid){
return userDAO.finByid(userid);
}
}
controller
package com.kgc.ssmdemo2.controller;
import com.kgc.ssmdemo2.entity.Userinfos;
import com.kgc.ssmdemo2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author:J
* @date:2019/11/13
* @aim:
*/
@Controller
public class InitCtrlk {
@Autowired
private UserService userService;
/**
*接受前端页面传送出来的日期类型 并进行转换
*/
@InitBinder
public void dateHanlder(WebDataBinder binder){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
sdf.setLenient(true);
binder.registerCustomEditor(Date.class,new CustomDateEditor(sdf,true));
}
@RequestMapping("/init.do")
public String init(HttpServletRequest request){
request.setAttribute("infos",userService.findAll());
return "index.jsp";
}
@RequestMapping("/save.do")
public String save(Userinfos user){
userService.saveUser(user);
return "redirect:/init.do";
}
@RequestMapping("/del.do")
public String del(int userid){
userService.deUser(userid);
return "redirect:/init.do";
}
@RequestMapping("/mod.do")
public String mod(Userinfos user){
userService.modUser(user);
return "redirect:/init.do";
}
@RequestMapping("/single.do")
public String single(int userid,HttpServletRequest request){
request.setAttribute("user",userService.finByid(userid));
return "user.jsp";
}
}
resources
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启自动配置-->
<context:annotation-config></context:annotation-config>
<!-- 扫描项目下所有使用注解的类型 -->
<context:component-scan base-package="com.kgc.ssmdemo2"></context:component-scan>
<!-- 2.数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 配置连接池属性 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost/mydemo"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="initialSize" value="3"></property>
<property name="maxActive" value="30"></property>
<property name="maxWait" value="30000"></property>
<property name="testOnBorrow" value="true"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置基于注解的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--整合spring和mybatis-->
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置mybatis全局配置文件:mybatis.cfg.xml -->
<!--<property name="configLocation" value="classpath:mybatis.cfg.xml"></property>-->
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<!--<property name="mapperLocations" value="classpath*:mapper/*.xml"></property>-->
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.kgc.ssmdemo2.dao"></property>
</bean>
</beans>
web
开始搓邪恶的前端页面小程序,加油!
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--spring-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!--过滤器-->
<filter>
<filter-name>charset</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>charset</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--serlvet-->
<servlet>
<servlet-name>spring</servlet-name>
<!--拦截下来的请求,依据相应的规则分发到目标Controller来处理-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<!--截获请求,将符合*.do格式的资源的请求都由指定的servlet处理-->
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
留意这里注释掉的地方,掌握这些小技巧,40分钟不是梦!
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: mu_bai
Date: 2019/11/13
Time: 22:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--div.header>a[href='save.jsp']{创建新用户} 打完按tab键,感受快乐--%>
<div class="header"><a href="save.jsp">创建新用户</a></div>
<%--div.ctx>table.table>(tr>th*4)+(tr>td*4)--%>
<div class="ctx">
<table class="table">
<tr>
<th>用户编号</th>
<th>用户姓名</th>
<th>用户生日</th>
<th>用户操作</th>
</tr>
<c:forEach items="${infos}" var="us">
<tr>
<td>${us.userid}</td>
<td>${us.username}</td>
<td>${us.birthday}</td>
<td>
<%--a[href='single.do?userid=']{修改}*2--%>
<a href="single.do?userid=${us.userid}">修改</a>
<a href="del.do?userid=${us.userid}">删除</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
save.jsp
<%--
Created by IntelliJ IDEA.
User: mu_bai
Date: 2019/11/13
Time: 22:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--div.ctx>form[action='save.do' method='post']>(label{用户}+input[type='text' name='']+br)*3--%>
<div class="ctx">
<form action="save.do" method="post">
<label>用户姓名</label>
<input type="text" name="username"><br>
<label>用户生日</label>
<input type="date" name="birthday"><br>
<input type="submit" value="提交用户"><br>
</form>
</div>
</body>
</html>
user.jsp
<%--
Created by IntelliJ IDEA.
User: mu_bai
Date: 2019/11/13
Time: 23:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<div class="ctx">
<form action="mod.do" method="post">
<input type="hidden" name="userid" value="${user.userid}">
<label>用户姓名</label>
<input type="text" name="username" value="${user.username}"><br>
<label>用户生日</label>
<input type="date" name="birthday" value="${user.birthday}"><br>
<label>用户</label>
<input type="submit" value="修改用户"><br>
</form>
</div>
</body>
</html>
最后
部署Tomcat
40分钟,一套用SSM框架完成的CURD打完收功!!
彩蛋
卑微博主在敲第9遍的时候,成功在40分钟内把这套SSM简单的哼哼嘿哈打完收工,傻乐半天QAQ!
关于SSM框架之前整合的一些困惑的地方,竟然有了些明悟,果然框架这边上手练才是进步最快的方式。
就像老陈说的,敲20遍,你就成神了,加油!