SSM——整合资源完成基础操作

搭建环境测试:

maven坐标导入:

<dependencies>
        <!--Junit-->
        <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        </dependency>
        <!--数据库驱动-->
      <dependency>
       <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.47</version>
        </dependency>
        <!-- 数据库连接池 -->
      <dependency>
         <groupId>com.mchange</groupId>
         <artifactId>c3p0</artifactId>
         <version>0.9.5.4</version>
      </dependency>

      <!--Servlet - JSP -->
       <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      </dependency>
        <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
        <version>2.2</version>
        </dependency>
        <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        </dependency>

        <!--Mybatis-->
        <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
        </dependency>
        <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.2</version>
        </dependency>

        <!--Spring-->
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.4</version>
        </dependency>

    </dependencies>

解决静态资源导出问题:

<build>
             <resources>
                   <resource>
                         <directory>src/main/java</directory>
                         <includes>
                               <include>**/*.properties</include>
                               <include>**/*.xml</include>
                         </includes>
                         <filtering>false</filtering>
                   </resource>
                   <resource>
                         <directory>src/main/resources</directory>
                         <includes>
                               <include>**/*.properties</include>
                               <include>**/*.xml</include>
                         </includes>
                         <filtering>false</filtering>
                   </resource>
             </resources>
    </build>

创建表和实体类:
在这里插入图片描述

    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;

编写配置文件:

mybatis-config.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>
        <package name="com.dxy.pojo"/>
    </typeAliases>

    <!--mapper进行映射注解-->
    <mappers>
        <mapper resource="com/dxy/dao/BookMapper.xml"></mapper>
    </mappers>

</configuration>

spring-dao.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"
       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">

    <!--配置整合mybatis-->

    <!--1.关联数据库文件 -->
    <context:property-placeholder location="classpath:database.properties"/>

    <!--2.配置数据库连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
    </bean>

    <!--配置sqlSessionFactory对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--配置mybatis全局配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>


    <!--动态扫描
        配置扫描Dao接口包,动态实现Dao接口注入到spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

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


</beans>

spring-service.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"
       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">


    <!--开启注解扫描Service层-->
    <context:component-scan base-package="com.dxy.service"></context:component-scan>

    <!--注入到IOC容器中-->
    <bean id="bookServiceImpl" class="com.dxy.service.BookServiceImpl">
        <!--set的方式,-->
        <property name="bookMapper" ref="bookMapper"/>
    </bean>

    <!--创建事务管理-->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置aop事务管理-->

</beans>

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

    <!--开启spring注解驱动-->
    <mvc:annotation-driven/>
    <!--开启静态资源配置-->
    <mvc:default-servlet-handler/>

    <!--开启controller包进行注解扫描-->
    <context:component-scan base-package="com.dxy.controller"></context:component-scan>

    <!--配置视图-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

database.properties

jdbc.username=root
jdbc.password=0000
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8

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

    <import resource="spring-mvc.xml"></import>
    <import resource="spring-dao.xml"></import>
    <import resource="spring-service.xml"></import>


</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!--配置DispatcherServlet-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--解决乱码问题-->
    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--设置session过期时间-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>


</web-app>

编写代码实现

BookMapper

public interface BookMapper {

    /*增加一条*/
    int add(Books books);

    /*删除一条*/
    int  delete(@Param("bookID") int id);

    /*修改一条数据*/
    int update(Books books);

    /*查询一条数据*/
    Books queryById(@Param("bookID") int id);

    /*查询全部数据*/
    List<Books> queryList();

    /*用于增加搜索功能*/
    List<Books> queryBooks(@RequestParam("bookName") String string);
}

BookMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.dxy.dao.BookMapper">

    <!--增加一条语句-->
    <insert id="add" parameterType="books">
        insert into books(bookName,bookCounts,detail)
        values(#{bookName},#{bookCounts},#{detail})
    </insert>

    <!--删除一条-->
    <delete id="delete">
        delete from books where bookID=#{bookID}
    </delete>

    <!--修改一条数据-->
    <update id="update" parameterType="books">
        update books set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
        where bookID=#{bookID}
    </update>

    <!--查询一条-->
    <select id="queryById" parameterType="int" resultType="books">
        select * from books where bookID=#{bookID}
    </select>

    <!--查询全部-->
    <select id="queryList" resultType="books">
        select * from books
    </select>

    <!--增加的搜索功能-->
    <select id="queryBooks" resultType="Books">
        select * from books
        where bookName like "%"#{bookName}"%"
    </select>

</mapper>

再去编写业务层和mvc层就ok了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值