一.安装jar包
jar包和ssm框架的包一样
需要commons-dbcp-1.4.jar,commons-logging-1.1.1.jar,commons-pool-1.6.jar,log4j-1.2.17.jar,mybatis-3.4.6.jar,spring-context-4.3.9.RELEASE.jarmybatis-spring-1.3.2.jar,mysql-connector-java-5.1.45-bin.jar,spring-aop-4.3.9.RELEASE.jar,spring-beans-4.3.9.RELEASE.jar,spring-core-4.3.9.RELEASE.jar,spring-expression-4.3.9.RELEASE.jar,spring-jdbc-4.3.9.RELEASE.jar,spring-tx-4.3.9.RELEASE.jar,spring-web-4.3.9.RELEASE.jar,spring-webmvc-4.3.9.RELEASE.jar
二.设计数据库
由于只实现简单的博客系统,
所以创建blog数据库
数据库中创建3个表,分别是user表,blog表,comment表
user的字段{
Uid int,
username varchar(255),
password varchar(255),
star int
}
blog表的字段{
Bid int,
blogname varchar(255),
Uid int,
username varchar(255),
star int,
content varchar(255)
}
其中UID是外键主表是user
comment表的字段{
Cid int,
Bid int,
Uid int,
username varchar(255),
content varchar(255)
}
外键是uid,bid主表是blog和user
三.创建项目
创建web项目将所需要的包导入到webcontent/web-INF/lib路径下
然后在web-INF中创建web.xml文件
web.xml
<?xml version="1.0" encoding="UTF-8"?><!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<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" id="WebApp_ID" version="3.0">
</web-app>
将这些写入web.xml中
四.创建模型类
user
package org.model;
import org.springframework.stereotype.Component;
@Component //将user类放入IOC 容器
public class user {
private int Uid;
private String name;
private String password;
private int star;
public int getUid() {
return Uid;
}
public void setUid(int uid) {
Uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getStar() {
return star;
}
public void setStar(int star) {
this.star = star;
}
}
下面的blog类,和comment类和user类相同
五.创建DAO层server层和controller层
创建基础Dao实现user的增删改查
创建useralter接口
package org.mapper;
import org.model.user;
public interface useralter {
public void adduser(user u);
public void deleteByid(int i);
public user queryuserByname(String name);
public void updateuserByid(int i);
}
创建useraltermapper.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="org.mapper.useralter">
<!-- namespace中写的是接口的路径-->
<select id="queryuserByname" parameterType="java.lang.String" resultType="org.model.user"> <!-- id的值和接口中方法的名字相同-->
select *from user where username=#{name}
</select>
<insert id="adduser" parameterType="org.model.user">
insert into user values(null,#{name},#{password},null)
</insert>
</mapper>
创建server类
package serve;
import org.mapper.useralter;
import org.model.user;
import org.springframework.stereotype.Component;
@Component //将server类加入IOC容器
public class userserve {
private useralter alter;
public void setAlter(useralter alter) {
this.alter = alter;
}
public void add( user u) {
alter.adduser(u);//这里的方法直接匹配到useraltermapper中的sql语句所以需要接口名和
mapper中的id一致
}
public void delete(int id) {
alter.deleteByid(id);
}
public user query(String name) {
return alter.queryuserByname(name);
}
public void update(int i){
alter.updateuserByid(i);
}
}
创建usercontroller
package controller;
import org.model.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import serve.userserve;
@Controller
@RequestMapping("user")//接收user请求
public class usercontroller {
@Autowired//自动装配
private userserve user;
@RequestMapping("login")//接收login请求,访问login方法时候需要发出user/login请求
public String login(String username,String password,ModelMap map){
if(user.query(username).getPassword().equals(password)){
return "redirect:/views/success.jsp";
}else {
return "redirect:/views/login.jsp";
}
}
@RequestMapping("sign")//接收sign请求,
public String sign (String username,String password1) {
user u=new user();
u.setName(username);
u.setPassword(password1);
user.add(u);//调用userserve类的add方法
return "redirect:/views/login.jsp";
}
}
六.配置文件的编写
application.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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.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">
<bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!--属性文件的位置-->
<property name="locations">
<list>
<value>classpath:DB.properties</value>
</list>
</property>
</bean>
<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>
</bean>
<bean id="sqlsession" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-产生sqlsession对象-->
<property name="dataSource" ref="datasource"></property>
<property name="mapperLocations" value="classpath:org/mapper/*.xml"></property>
<!-mapper文件的位置 -->
</bean>
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlsession"></property>
<property name="basePackage" value="org.mapper"></property><!-产生包里的mapper对象-->
</bean>
<bean id="userserve" class="serve.userserve">
<property name="alter" ref="useralter"></property><!--注入useralter对象-->
</bean>
</beans>
application-controller.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:component-scan base-package="controller"></context:component-scan>
<!--扫描包中的注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!--springmvc中的标配防止出现冲突-->
<mvc:default-servlet-handler/>
<!--在请求时如果在controller中没有找到转入MVC方式-->
</beans>
web.xml的编写
<?xml version="1.0" encoding="UTF-8"?><!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<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" id="WebApp_ID" version="3.0">
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<context-param><!--说明application.xml的位置-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param>
<listener>
<!-- 配置监听器监听在tomcat启动时创建IOC-->
<listener-class>org.springframework.web.context.ContextLoaderListener</listenerclass>
</listener>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param><!--说明spring-mvc配置文件的位置-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-controller.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>/</url-pattern><!--拦截所有请求将请求交给spring-mvc-->
</servlet-mapping>
</web-app>
七.前台页面的编写
login.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>
<style>
body{
width:100%;
height:100%;
margin:0;
padding:0;
background-color:#f5f7ff;
padding-top: 20px;
}
#q{
width:100%;
height:100%;
overflow:hidden;
}
#left{
height:100%;
width:207px;
float:left;
overflow:hidden;
}
#right{
height:100%;
width:207px;
float:right;
overflow:hidden;
}
#sign{
width:420px;
height:421px;
background-color: white;
float:top;
margin: 0 auto;
margin-top: -20px;
border-style: solid;
border-width: 1px;
border-color: #E6E6E6;
}
#title{
width:150px;
margin: 0 auto;
margin-top: 0px;
}
img#title{
width:150px ;
height:100px;
}
#sign{
text-align: center;
text-decoration: none;
}
span#sign{
font-size: 15px;
}
.put{
margin-top: 5px;
width:340px;
height:30px;
margin-bottom: 5px;
background-color: #fff;
border-radius:2px;
border-width: 1px;
padding:2px 4px 3px 4px;
}
.but{
margin-top:10px ;
width:350px;
height:48px;
background-color: #0060df;
border-width:1px;
border-radius:4px;
color:white;
}
</style>
<body>
<div id="q">
<div id="left">
<img src="../images/left.png" height="100%" width="100%">
</div>
<div id="right">
<img src="../images/right.png" height="100%" width="100%">
</div>
<div id="title">
<img id="title" src="../images/title.png">
</div>
<div id="sign">
<h1>登录</h1>
<span>以登录 YourBlog</span><br/>
<form action="../user/login" method="get" accept-charset="utf-8">
<input class="put" type="text" name="username" placeholder="用户名"><br/>
<input class="put" type="password" name="password" placeholder="密码"><br/>
<input class="but" type="submit" value="登录"><br/>
<a href="regiter.jsp">创建用户</a>
</form>
</div>
</div>
</body>
<html>
register.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>
<style>
body{
width:100%;
height:100%;
margin:0;
padding:0;
background-color:#f5f7ff;
padding-top: 20px;
}
#q{
width:100%;
height:100%;
overflow:hidden;
}
#left{
height:100%;
width:207px;
float:left;
overflow:hidden;
}
#right{
height:100%;
width:207px;
float:right;
overflow:hidden;
}
#sign{
width:420px;
height:421px;
background-color: white;
float:top;
margin: 0 auto;
margin-top: -20px;
border-style: solid;
border-width: 1px;
border-color: #E6E6E6;
}
#title{
width:150px;
margin: 0 auto;
margin-top: 0px;
}
img#title{
width:150px ;
height:100px;
}
#sign{
text-align: center;
text-decoration: none;
}
span#sign{
font-size: 15px;
}
.put{
margin-top: 5px;
width:340px;
height:30px;
margin-bottom: 5px;
background-color: #fff;
border-radius:2px;
border-width: 1px;
padding:2px 4px 3px 4px;
}
.but{
margin-top:10px ;
width:350px;
height:48px;
background-color: #0060df;
border-width:1px;
border-radius:4px;
color:white;
}
</style>
<body>
<div id="q">
<div id="left">
<img src="../images/left.png" height="100%" width="100%">
</div>
<div id="right">
<img src="../images/right.png" height="100%" width="100%">
</div>
<div id="title">
<img id="title" src="../images/title.png">
</div>
<div id="sign">
<h1>创建</h1>
<span>以登录 YourBlog</span><br/>
<form action="../user/sign" method="get" accept-charset="utf-8">
<input class="put" type="text" name="username" placeholder="用户名"><br/>
<input class="put" type="password" name="password1" placeholder="密码"><br/>
<input class="put" type="password" name="password2" placeholder="确认密码"><br/>
<input class="but" type="submit" value="创建账户"><br/>
<a href="login.jsp">已有用户,登录</a>
</form>
</div>
</div>
</body>
</html>