ssm整合(一)

ssm整合思路

一.概述

ssm框架就是springMVC,spring,mybatis三者的整合

二.jar包的导入

1)spring相关jar

1.spring-context.jar// 这个jar文件为spring核心提供了大量的扩展。
2.spring-context-support.jar//spring-context的扩展支持,用于mvc方面
3.spring-bean.jar// 这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Ioc/DI操作相关的所有类
4.spring-core.jar//这个jar文件包含spring框架基本的核心工具类,spring其他的组件都要是使用到这个包里的类,是其他组件的基本核心
5.spring-web.jar// 这个jar文件包含web应用开发时,用到spring框架时所需要的核心类,
6.spring-tx.jar//spring提供对事务的支持,事务的相关处理以及实现类就在这个jar包中
7.spring-aspects.jar //提供对aspectj的支持,以便可以方便的将面向方面的功能集成进ide中
8.commons-logging.jar//用来记录程序运行时的活动的日志记录
9.aopalliance-1.0.jar
10.asm-3.3.1.jar
11.aspectjweaver-1.6.11.jar
12.spring-aop-4.1.3.RELEASE.jar
13.spring-expression-4.1.3.RELEASE.jar
14.spring-jdbc-4.1.3.RELEASE.jar
15.spring-jms-4.1.3.RELEASE.jar
16.spring-messaging-4.1.3.RELEASE.jar

2)springmvc相关jar

1.spring-webmvc.jar //这个jar文件包含Spring MVC框架相关的所有类

3)mybatis相关jar

1.mybatis.jar//实现mybatis框架的工具包。
2.mybatis-spring.jar//集合spring与mybatis框架的工具包
3.mysql-connector-java.jar//mysql数据库链接java 的jdbc 数据库引擎的jar包
4.cglib.jar//全称是Code Generation Library

4)其他jar

1.junit-jar //junit测试相关jar
2.fastjson.jar//JSON数据转换
3.jstl-1.2.jar
4.druid-1.1.9.jar//阿里巴巴druid连接池
5.log4j-1.2.17.jar//日志相关jar
6.slf4j-api-1.7.5.jar

 

三.代码实现

1)controller层

@Controller//声明将这个事务交给spring容器管理
@RequestMapping("/item")//当这个注解放到类上,表示以后的请求路径都需要带上这个前缀
public class ItemController {
​
    @Autowired//自动装配,将由spring容器管理的对象交给这个对象
    private ItemService itemService;
​
    //@RequestMapping代表了前台页面访问这个controller类里方法的路径
    @RequestMapping(value = "/itemList.action")
    public ModelAndView queryItem() throws Exception {
        List<Items> list = itemService.queryItemList();
        // 创建ModelAndView,用来存放数据和视图
        ModelAndView mav = new ModelAndView();
        // 设置数据到模型中
        mav.addObject("itemList", list);
        // 设置视图jsp,需要设置视图的物理地址
        mav.setViewName("itemList");
        return mav;
    }

2)service层

1.接口层
​
public interface ItemService {
    
    List<Items> queryItemList() ;
}
​
2.实现层
@Service//表名这个类是业务层,并将其交给spring管理
@Transactional//开启事务
public class ItemServiceImpl implements ItemService {
​
    @Autowired//自动装配业务层代码
    private ItemsMapper itemsMapper;
    
    @Override//重写了接口里的方法
    public List<Items> queryItemList() {
        //调用了业务层生成的map方法
        List<Items> list = itemsMapper.selectByExample(null);
        return list;
    }
}

3)dao层

/**
* 这里我使用了mybatis的逆向工程生成我的mapper接口
*/
public interface ItemsMapper {
    int countByExample(ItemsExample example);
​
    int deleteByExample(ItemsExample example);
    //通过主键删除
    int deleteByPrimaryKey(Integer id);
    //插入记录
    int insert(Items record);
    
    int insertSelective(Items record);
​
    List<Items> selectByExampleWithBLOBs(ItemsExample example);
    //通过example有条件的查询
    List<Items> selectByExample(ItemsExample example);
    //通过主键查询
    Items selectByPrimaryKey(Integer id);
    
    int updateByExampleSelective(@Param("record") Items record, @Param("example") ItemsExample example);
​
    int updateByExampleWithBLOBs(@Param("record") Items record, @Param("example") ItemsExample example);
​
    int updateByExample(@Param("record") Items record, @Param("example") ItemsExample example);
​
    int updateByPrimaryKeySelective(Items record);
​
    int updateByPrimaryKeyWithBLOBs(Items record);
    //通过主键更新
    int updateByPrimaryKey(Items record);
}

4) pojo

//通过逆向工程生成的pojo类
package com.springmvc.pojo;
​
import java.util.Date;
​
public class Items {
    private Integer id;
​
    private String name;
​
    private Float price;
​
    private String pic;
​
    private Date createtime;
​
    private String detail;
    
    
​
    public Items(Integer id, String name, Float price, Date createtime, String detail) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
        this.createtime = createtime;
        this.detail = detail;
    }
​
    public Items() {
        super();
        // TODO Auto-generated constructor stub
    }
​
    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 == null ? null : name.trim();
    }
​
    public Float getPrice() {
        return price;
    }
​
    public void setPrice(Float price) {
        this.price = price;
    }
​
    public String getPic() {
        return pic;
    }
​
    public void setPic(String pic) {
        this.pic = pic == null ? null : pic.trim();
    }
​
    public Date getCreatetime() {
        return createtime;
    }
​
    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }
​
    public String getDetail() {
        return detail;
    }
​
    public void setDetail(String detail) {
        this.detail = detail == null ? null : detail.trim();
    }
}
//这里还有一个通过逆向工程生成的ItemExample类,辅助我们的查询
package com.springmvc.pojo;
​
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
​
public class ItemsExample {
    protected String orderByClause;
​
    protected boolean distinct;
​
    protected List<Criteria> oredCriteria;
​
    public ItemsExample() {
        oredCriteria = new ArrayList<Criteria>();
    }
​
    public void setOrderByClause(String orderByClause) {
        this.orderByClause = orderByClause;
    }
​
    public String getOrderByClause() {
        return orderByClause;
    }
​
    public void setDistinct(boolean distinct) {
        this.distinct = distinct;
    }
​
    public boolean isDistinct() {
        return distinct;
    }
​
   .....
    }
}

四.配置文件

1)web.xml

 <!-- springmvc前端控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定springMVC配置文件 -->
        <!-- SpringMVC的配置文件的默认路径是/WEB-INF/${servlet-name}-servlet.xml -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--spring前端控制器,拦截这些带有.action后缀的请求路径-->
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
​
    <!-- 解决post的中文乱码问题 -->
    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
​
    <!-- 加载配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-*.xml</param-value>
    </context-param>
    <!-- 配置listener,让spring容器随着项目的启动而初始化 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

2) log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
​
​3)druid.properties
# druid连接池会帮你自动匹配数据库驱动,所以可以不需要配置
druid.url=jdbc:mysql://localhost:3306/ssm
druid.username=root
druid.password=1234

4)SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
​
    <typeAliases>
        <!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
        <package name="com.springmvc.pojo" />
    </typeAliases>
</configuration>
5)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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.2.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    ">
​
    <!-- springMVC是spring的子项目.配置注解扫描即可 -->
    <context:component-scan base-package="com.springmvc" />
​
    <!-- 注解驱动 -->
    <mvc:annotation-driven  />
​
    <!-- 视图解析器 -->
    <!-- 以后访问的路径就是 = 前缀+逻辑视图+后缀 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置逻辑视图前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 配置逻辑视图后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
​
</beans>

6)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-4.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.2.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
    <!-- 加载事务的注解驱动 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <!-- 创建transactionManager -->
    <bean name="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- Mapper动态代理开发   扫描 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 基本包 -->
        <property name="basePackage" value="com.springmvc.mapper"/>
    </bean>
​
    <!-- 创建sqlSessionFactory对象 -->
    <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 需要一个数据源 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 需要一个resource -->
        <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
    </bean>
​
    <!-- 创建数据源 -->
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!-- 连接信息 -->
        <property name="url" value="${druid.url}" />
        <property name="username" value="${druid.username}" />
        <property name="password" value="${druid.password}" />
    </bean>
​
    <!-- 加载外部文件 -->
    <context:property-placeholder location="classpath:druid.properties" />
</beans>

7)ItemsMapper.xml

<!--这是通过mybatis逆向工程自动生成的Mapper.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="com.springmvc.mapper.ItemsMapper" >
  <resultMap id="BaseResultMap" type="com.springmvc.pojo.Items" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="price" property="price" jdbcType="REAL" />
    <result column="pic" property="pic" jdbcType="VARCHAR" />
    <result column="createtime" property="createtime" jdbcType="TIMESTAMP" />
  </resultMap>
  <resultMap id="ResultMapWithBLOBs" type="com.springmvc.pojo.Items" extends="BaseResultMap" >
    <result column="detail" property="detail" jdbcType="LONGVARCHAR" />
  </resultMap>
    ......
</mapper>

五.总结

一个ssm的整合基本完成,里面运用了mybatis的逆向工程,帮助我们生成pojo类,和相关的mapper接口和Mapper.xml文件,简化了我们的操作,但是,这只是一个小demo,里面还有很多功能未实现.

 

......未完待续

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值