第一步:创建动态web项目
第二步:导入依赖包
第三步:配置db.properties
jdbc. driverClassName= com. mysql. jdbc. Driver
jdbc. url= jdbc: mysql: / / localhost: 3306 / gj1
jdbc. username= root
jdbc. password= 123456
jdbc. maxActive= 10
第四步:配置lo4j.properties
# Global logging configuration
log4j. rootLogger= ERROR, stdout
# MyBatis logging configuration. . .
# 配置日志输入 : log4j. logger. 映射文件的包 = 日志级别5 个(error,warin,info,debug,all/ trace)
log4j. logger. cn. zj. ssm. mapper= all
# Console output. . .
log4j. appender. stdout= org. apache. log4j. ConsoleAppender
log4j. appender. stdout. layout= org. apache. log4j. PatternLayout
log4j. appender. stdout. layout. ConversionPattern= % 5 p [ % t] - % m% n
第五步:配置springdao.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: 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- 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 = "cn.zj.ssm" / >
< ! -- 读取db. properties配置文件 -- >
< context: property- placeholder location= "classpath:db.properties" / >
< ! -- 配置数据源 -- >
< bean id= "dataSource" class = "com.alibaba.druid.pool.DruidDataSource" init- method= "init" destroy- method= "close" >
< property name= "driverClassName" value= "${jdbc.driverClassName}" / >
< property name= "url" value= "${jdbc.url}" / >
< property name= "username" value= "${jdbc.username}" / >
< property name= "password" value= "${jdbc.password}" / >
< property name= "maxActive" value= "${jdbc.maxActive}" / >
< / bean>
< ! -- 配置MyBatis框架的 SqlSessionFactory 工厂对象 -- >
< bean name= "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" >
< ! -- 注入数据源 -- >
< property name= "dataSource" ref= "dataSource" / >
< ! -- 配置别名 -- >
< property name= "typeAliasesPackage" value= "cn.zj.ssm.pojo" / >
< / bean>
< ! -- 使用包扫描创建映射接口的代理对象 -- >
< bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" >
< ! -- 配置映射接口对应的包 -- >
< property name= "basePackage" value= "cn.zj.ssm.mapper" / >
< ! -- 配置工厂bean的名称 【可选】 -- >
< property name= "sqlSessionFactoryBeanName" value= "sqlSessionFactory" / >
< / bean>
< ! -- 事务配置
1. 配置事务管理器
2. 配置事务通知
3. 使用AOP将事务切入到Service层
-- >
< bean id= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< ! -- 注入数据源 -- >
< property name= "dataSource" ref= "dataSource" / >
< / bean>
< tx: advice id= "txAdvice" transaction- manager= "transactionManager" >
< tx: attributes>
< ! -- DQL -- >
< tx: method name= "select*" read- only= "true" / >
< ! -- DML -- >
< tx: method name= "*" / >
< / tx: attributes>
< / tx: advice>
< aop: config>
< ! -- 配置切面 = 切入点+ 通知 -- >
< aop: advisor advice- ref= "txAdvice" pointcut= "execution(* cn.zj.ssm.service..*.*(..))" / >
< / aop: config>
< / beans>
第六步:配置springmvc.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"
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">
< ! -- 设置注解驱动 -- >
< mvc: annotation- driven/ >
< ! -- 配置视图解析器 -- >
< bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name= "prefix" value= "/WEB-INF/view/" / >
< property name= "suffix" value= ".jsp" / >
< / bean>
< ! -- 导入其他配置文件 -- >
< ! -- < import resource= "classpath:spring.xml" / > -- >
< / beans>
第七步:创建pojo
package cn. zj. ssm. pojo;
public class User {
private Integer id;
private String name;
private String phone;
private String password;
private Integer age;
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public String getPhone ( ) {
return phone;
}
public void setPhone ( String phone) {
this . phone = phone;
}
public String getPassword ( ) {
return password;
}
public void setPassword ( String password) {
this . password = password;
}
public Integer getAge ( ) {
return age;
}
public void setAge ( Integer age) {
this . age = age;
}
@Override
public String toString ( ) {
return "User [id=" + id + ", name=" + name + ", phone=" + phone + ", password=" + password + ", age=" + age
+ "]" ;
}
}
第八步:创建UserMapper.java
package cn. zj. ssm. mapper;
import java. util. List;
import org. apache. ibatis. annotations. Delete;
import org. apache. ibatis. annotations. Insert;
import org. apache. ibatis. annotations. Select;
import org. apache. ibatis. annotations. Update;
import cn. zj. ssm. pojo. User;
public interface UserMapper {
@Select ( "select * from user where id = #{id}" )
public User selectByPrimaryKey ( Integer id) ;
@Select ( "select * from user" )
public List< User> selectList ( ) ;
@Insert ( "insert into user(name,age,password,phone)values(#{name},#{age},#{password},#{phone})" )
public int insert ( User user) ;
@Update ( "update user set name = #{name},age=#{age},phone=#{phone},password=#{password} where id = #{id}" )
public int updateByPrimaryKey ( User user) ;
@Delete ( "delete from user where id = #{id}" )
public int deleteByPrimaryKey ( Integer id) ;
}
第九步:配置UserServiceImpl
package cn. zj. ssm. service. impl;
import java. util. List;
import javax. annotation. Resource;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Service;
import cn. zj. ssm. mapper. UserMapper;
import cn. zj. ssm. pojo. User;
import cn. zj. ssm. service. UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User selectByPrimaryKey ( Integer id) {
return userMapper. selectByPrimaryKey ( id) ;
}
@Override
public List< User> selectList ( ) {
return userMapper. selectList ( ) ;
}
@Override
public int insert ( User user) {
return userMapper. insert ( user) ;
}
@Override
public int updateByPrimaryKey ( User user) {
return userMapper. updateByPrimaryKey ( user) ;
}
@Override
public int deleteByPrimaryKey ( Integer id) {
return userMapper. deleteByPrimaryKey ( id) ;
}
}
第十步:配置UserController
package cn. zj. ssm. controller;
import java. util. List;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Controller;
import org. springframework. ui. Model;
import org. springframework. web. bind. annotation. RequestMapping;
import cn. zj. ssm. pojo. User;
import cn. zj. ssm. service. UserService;
@Controller
@RequestMapping ( "/user" )
public class UserController {
@Autowired
private UserService userService;
@RequestMapping ( "/list" )
public String list ( Model m) {
List< User> users = userService. selectList ( ) ;
m. addAttribute ( "users" , users) ;
return "userList" ;
}
@RequestMapping ( "/delete" )
public String delete ( Integer userId) {
userService. deleteByPrimaryKey ( userId) ;
return "redirect:/user/list.do" ;
}
}
第十一步:配置web.xml
< ? 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" version= "3.1" >
< ! -- 配置前端控制器(总控) -- >
< ! -- 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>
< ! --
如何读取多个配置文件,两种解决方案
方案一:
在一个配置文件中使用 < import > 导入其他配置,此处读取主配置文件即可
方式二:
所有配置文件同一前缀,使用 星号(* )通配符读取
spring* . xml
-- >
< param- value> classpath: spring* . 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> * . do < / url- pattern>
< / servlet- mapping>
< / web- app>
第十二步:配置userList.jsp
< % @ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" % >
< ! -- 引入jstl标签库 -- >
< % @ taglib uri= "http://java.sun.com/jsp/jstl/core" prefix= "c" % >
< ! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> Insert title here< / title>
< / head>
< body>
< h3> 用户列表< / h3>
< table border= "1" style= "border-collapse: collapse;" >
< tr>
< th> id< / th>
< th> name< / th>
< th> password< / th>
< th> age< / th>
< th> phone< / th>
< th> 操作< / th>
< / tr>
< c: forEach items= "${users}" var= "user" varStatus= "vs" >
< tr style= "${vs.index % 2 eq 0 ? 'background-color: gray':''}" >
< td> ${ user. id} < / td>
< td> ${ user. name} < / td>
< td> ${ user. password} < / td>
< td> ${ user. age} < / td>
< td> ${ user. phone} < / td>
< td>
< a onclick= "deleteUser(${user.id})" href= "javascript:void(0);" > 删除< / a> & nbsp; & nbsp;
< a href= "#" > 修改< / a>
< / td>
< / tr>
< / c: forEach>
< / table>
< script type= "text/javascript" >
function deleteUser ( userId) {
if ( confirm ( "亲,您确定要删除此条数据么??" ) ) {
window. location. href= "${pageContext.request.contextPath}/user/delete.do?userId=" + userId
}
}
< / script>
< / body>
< / html>
第十三步:测试结果