JAVA练习---SSM框架整合

SSM框架整合

近期学习了Sring框架、SpringMVC框架、mybatis框架。最后对这三个框架进行整合操作,下面记录完整的搭建过程,方便日后学习浏览。
为了测试简单,我只写了一个findAll()方法,来查询数据表中的所有数据,如果数据能全部响应到页面中,说明SSM框架搭建成功。

1,基于Maven的项目结构

JAVA练习---SSM框架整合_第1张图片

2,导入相关依赖

pom.xml


<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.0modelVersion>

    <groupId>com.qfgroupId>
    <artifactId>SSM2artifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>warpackaging>
    <properties>
        <spring.version>4.3.5.RELEASEspring.version>
        <mybatis.version>3.2.7mybatis.version>
    properties>
    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>
        
        <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-ormartifactId>
        <version>${spring.version}version>
    dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>aopalliancegroupId>
            <artifactId>aopallianceartifactId>
            <version>1.0version>
        dependency>
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.9.2version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>${mybatis.version}version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.3.2version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.47version>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.1.10version>
        dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.9.8version>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.0version>
        dependency>
        
        <dependency>
            <groupId>jstlgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>



    dependencies>


project>

3,配置web.xml


<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"
         id="WebApp_ID" version="3.1">
    
   <context-param>
       <param-name>contextConfigLocationparam-name>
       <param-value>classpath:applicationContext*.xmlparam-value>
   context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc-servlet.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
    
    <filter>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>utf-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>


web-app>

4,配置applicationContext.xml(spring配置文件)


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="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/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    

    
    <context:property-placeholder location="classpath:dbinfo.properties">context:property-placeholder>
    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}">property>
        <property name="password" value="${jdbc.password}">property>
        <property name="username" value="${jdbc.username}">property>
        <property name="url" value="${jdbc.url}">property>
    bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource">property>
        <property name="mapperLocations" value="classpath:/mapper/*.xml">property>
    bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="basePackage" value="com.xh.mapper">property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
    bean>


    <context:component-scan base-package="com.xh.service">context:component-scan>




beans>

5,配置springmvc-servlet.xml(springmvc配置文件)


<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: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">

    
    <context:component-scan base-package="com.xh.controller">context:component-scan>

    
    <mvc:annotation-driven>mvc:annotation-driven>

    
    <mvc:default-servlet-handler>mvc:default-servlet-handler>
beans>

6,定义实体类(Product)

public class Product {
    private int pid;
    private int id;
    private String pname;
    private Date ptime;
    private String pinfo;
    private BigDecimal pprice;
    private int pstate;
    private String pimage;

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public Date getPtime() {
        return ptime;
    }

    public void setPtime(Date ptime) {
        this.ptime = ptime;
    }

    public String getPinfo() {
        return pinfo;
    }

    public void setPinfo(String pinfo) {
        this.pinfo = pinfo;
    }

    public BigDecimal getPprice() {
        return pprice;
    }

    public void setPprice(BigDecimal pprice) {
        this.pprice = pprice;
    }

    public int getPstate() {
        return pstate;
    }

    public void setPstate(int pstate) {
        this.pstate = pstate;
    }

    public String getPimage() {
        return pimage;
    }

    public void setPimage(String pimage) {
        this.pimage = pimage;
    }
}

7,定义mapper接口

为了简单,只用SSM测试一个方法

public interface ProductMapper {
    public List<Product> findAll(); //我们在这只展示这一个方法,测试能否在SSM框架中测试成功
}

8,定义mapper映射文件




<mapper namespace="com.xh.mapper.ProductMapper">
    <select id="findAll" resultType="com.xh.entity.Product">
        select  p_id as pid,t_id as id,p_name as pname,p_time as ptime,p_info as pinfo ,p_price as pprice, p_state as pstate,p_image as pimage
        from product
    select>
mapper>

9,定义service接口

public interface ProductService {
    public List<Product> findAll(); //我们在这只展示这一个方法,测试能否在SSM框架中测试成功
}

10,实现service接口

@Service
public class ProductServiceImpl implements ProductService {
    //利用spring注解对其进行注入
    @Resource()
    private ProductMapper productMapper;


    @Override
    public List<Product> findAll() {
        return productMapper.findAll();
    }
}

11,创建ProductController

@Controller
public class ProductController {
    @Autowired
    private ProductServiceImpl productService;



    @RequestMapping("/findAll") //请求路径
    @ResponseBody //将后端的数据转换为json,并响应到字符串中
    public List<Product> findAll(){
        return productService.findAll();
    }
}

12,请求访问

在这里插入图片描述

13,测试成功

通过发送请求,数据库中的数据成功的转为json数据,并且响应到了页面上,由此可知SSM框架搭建成功。
JAVA练习---SSM框架整合_第2张图片

你可能感兴趣的:(JAVA框架,mybatis,spring,java,maven)