1.环境的搭建
1.spring+springmvc+mybatis框架所需的必要架包:
链接:https://pan.baidu.com/s/12-dXvzgDbXC_NdCazmqTNg
提取码:bmbg
2.创建一个普通的web项目,先将本项目所需要的所有架包全部导入:
详细步骤:将下载好的lib包放入WEB-INF包,后右键lib包,点击Add Library
3.右键项目,创建一个存放配置文件的文件夹config,并右键选择Mark Directory as ——》sources root,将此文件夹设置问根文件夹
4.先创建名为jdbc.properties的数据库配置文件(url,driver,user,password的配置):
jdbc.properties
jdbc.url = jdbc:mysql://localhost:3306/ssm?rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8
jdbc.driver = com.mysql.jdbc.Driver
jdbc.username= root
jdbc.password=
根据自己库版本和数据库的用户名和密码修改
5.配置spring核心配置文件:
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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--读取jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--创建dataSource数据源-->
<!--创建dataSource数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="10"/><!--最大连接数-->
<property name="maxIdle" value="5"/><!--最大空闲数-->
</bean>
<!-- 创建SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 关联连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 加载sql映射文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!-- 引入插件 -->
<property name="plugins">
<array>
<!-- mybatis分页插件 -->
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--
helperDialect:连接数据库的类型
-->
<value>
helperDialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean>
<!-- Mapper接口的扫描 -->
<!--
注意:如果使用Mapper接口包扫描,那么每个Mapper接口在Spring容器中的id名称为类名: 例如 CustomerMapper -> customerMapper
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置Mapper接口所在包路径 -->
<property name="basePackage" value="cn.sm1234.dao"/>
</bean>
<!-- 开启Spring的IOC注解扫描 -->
<context:component-scan base-package="cn.sm1234"/>
<!-- 开启Spring的事务 -->
<!-- -事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 启用Spring事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
6.创建log4j配置文件
log4j.properties
log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
7.配置spring-mvc配置文件
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:contenxt="http://www.springframework.org/schema/context" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描controller所在的包-->
<contenxt:component-scan base-package="cn.sm1234.controller"/>
<!--注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--视图解析器:简化在Controller-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!--后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml配置
<?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>01.mybatis</display-name>
<!-- 配置SpringMVC编码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 启动SpringMVC -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 参数:读取spring-mvc.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 启动spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 修改路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<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>
到这步,环境配置就基本搭建好了
前端页面的准备:
链接:https://pan.baidu.com/s/1FN9N7yLmyUwISwxVciguWA
提取码:1ok0
1.将下载好的前端页面导入web包下
后端的编写:
1.在src包下创建cn.sm1234包,并在sm1234包下创建4个包:controller,service,dao,domain
2.dao包下domain实体类的编写
Customer
package cn.sm1234.domain;
public class Customer {
private Integer id;
private String name;
private String gender;
private String telephone;
private String address;
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", telephone='" + telephone + '\'' +
", address='" + address + '\'' +
'}';
}
}
dao层的编写
CustomerMapper
package cn.sm1234.dao;
import cn.sm1234.domain.Customer;
import java.util.List;
public interface CustomerMapper {
public List<Customer> findAll();
}
service的编写
CustomerService
package cn.sm1234.service.impl;
import cn.sm1234.dao.CustomerMapper;
import cn.sm1234.domain.Customer;
import cn.sm1234.service.CustomerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
@Service("CustomerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {
@Resource
private CustomerMapper customerMapper;
@Override
public List<Customer> findAll() {
return customerMapper.findAll() ;
}
}
**实现类的编写
CustomerServiceImpl
package cn.sm1234.service.impl;
import cn.sm1234.dao.CustomerMapper;
import cn.sm1234.domain.Customer;
import cn.sm1234.service.CustomerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
@Service("CustomerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {
@Resource
private CustomerMapper customerMapper;
@Override
public List<Customer> findAll() {
return customerMapper.findAll() ;
}
}
controller控制器的编写
CustomerController
package cn.sm1234.service.impl;
import cn.sm1234.dao.CustomerMapper;
import cn.sm1234.domain.Customer;
import cn.sm1234.service.CustomerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
@Service("CustomerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {
@Resource
private CustomerMapper customerMapper;
@Override
public List<Customer> findAll() {
return customerMapper.findAll() ;
}
}
运行tomcat,这时候一个简单的客户展示功能就实现了
客户的增加,修改与批量删除的实现:
1.用户的增加功能的实现
在CustomerController中添加增加方法
@RequestMapping("save")
@ResponseBody
public Map<String,Object> save(Customer customer){
try {
customerService.save(customer);
result.put("success",true);
}catch (Exception e){
e.printStackTrace();
result.put("success",false);
result.put("msg:",e.getMessage());
}
finally {
return result;
}
}
在CustomerService和CustomerServiceImpl添加save方法
void save(Customer customer);
@Override
public void save(Customer customer) {
customerMapper.save(customer);
}
在CustomerMapper中添加save方法
void save(Customer customer);
在CustomerMapper.xml添加保存的sql
<insert id="save" parameterType="cn.sm1234.domain.Customer">
insert into t_customer(name,gender,telephone,address) values (#{name},#{gender},#{telephone},#{address})
</insert>
2.客户修改功能的实现
.。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。以后再写