Spring学习笔记(篇四——SSM框架整合)

代码地址: https://github.com/Zhuyaqiang/spring-study

目录

    • 15.1 环境搭建
      • 15.1.1 建表
      • 15.1.2 建maven项目, 导入依赖, 配置Maven资源过滤
      • 15.1.3 配置数据源
      • 15.1.4 编写mybatis配置文件`mybatis-config.xml`
    • 15.2 整合Mybatis
      • 15.2.1 POJO
      • 15.2.2 dao
      • 15.2.3 service
    • 15.3 Spring整合
      • 15.3.1 spring整合mybatis(spring-dao.xml)
      • 15.3.2 spring整合Service(spring-service.xml)
      • 15.3.3 spring整合springmvc(web.xml)
    • 15.4 编写业务

15.1 环境搭建

15.1.1 建表

CREATE TABLE `books` (
  `bookID` int(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
  `bookName` VARCHAR(100) NOT NULL COMMENT '书名',
  `bookCounts` INT(11) NOT NULL COMMENT '数量',
  `detail` VARCHAR(200) NOT NULL COMMENT '描述',
  KEY `bookID` (`bookID`) 
) ENGINE=INNODB DEFAULT CHARSET=utf;
insert into `books` (`bookID`, `bookName`, `bookCounts`, `detail`) values
(1,'Java',1,'从入门到放弃'),
(2,'MYSQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');

15.1.2 建maven项目, 导入依赖, 配置Maven资源过滤


<dependencies>
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
    dependency>
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>5.1.47version>
    dependency>
    
    <dependency>
        <groupId>com.mchangegroupId>
        <artifactId>c3p0artifactId>
        <version>0.9.5.2version>
    dependency>
    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>servlet-apiartifactId>
        <version>2.5version>
    dependency>
    <dependency>
        <groupId>javax.servlet.jspgroupId>
        <artifactId>jsp-apiartifactId>
        <version>2.2version>
    dependency>
    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>jstlartifactId>
        <version>1.2version>
    dependency>
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatisartifactId>
        <version>3.5.2version>
    dependency>
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatis-springartifactId>
        <version>2.0.4version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-jdbcartifactId>
        <version>4.1.0.RELEASEversion>
    dependency>
dependencies>

<build>
    <resources>
        <resource>
            <directory>src/main/javadirectory>
            <includes>
                <include>**/*.propertiesinclude>
                <include>**/*.xmlinclude>
            includes>
            <filtering>falsefiltering>
        resource>
        <resource>
            <directory>src/main/resourcesdirectory>
            <includes>
                <include>**/*.propertiesinclude>
                <include>**/*.xmlinclude>
            includes>
            <filtering>falsefiltering>
        resource>
    resources>
build>

15.1.3 配置数据源

jdbc.driver=com.mysql.jdbc.Driver
# 如果使用的是Mysql8.0+, 需要增加一个时区的配置
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
jdbc.username="root"
jdbc.password="0314"

15.1.4 编写mybatis配置文件mybatis-config.xml



<configuration>

    <typeAliases>
        <package name="com.zyq.pojo"/>
    typeAliases>
configuration>

15.2 整合Mybatis

15.2.1 POJO

编写实体类

15.2.2 dao

编写dao层数据查询接口和Mapper文件, 并在mybatis配置文件中注册该Mapper

15.2.3 service

编写service接口和实现类

15.3 Spring整合

15.3.1 spring整合mybatis(spring-dao.xml)


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

    <context:property-placeholder location="classpath:database.properties">context:property-placeholder>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>


        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>

        <property name="autoCommitOnClose" value="false"/>

        <property name="checkoutTimeout" value="10000"/>

        <property name="acquireRetryAttempts" value="2"/>
    bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    bean>


    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

        <property name="basePackage" value="com.zyq.dao"/>
    bean>

beans>

15.3.2 spring整合Service(spring-service.xml)


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


    <bean id="BookServiceImpl" class="com.zyq.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource"/>
    bean>

15.3.3 spring整合springmvc(web.xml)


<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:applicationContext.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    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>

    <session-config>
        <session-timeout>15session-timeout>
    session-config>
web-app>

15.4 编写业务

创建Controller, 调用bookservice(使用@Autowired@Qualifier进行注入和指定包), 增删改查

你可能感兴趣的:(学习笔记)