ssm整合,详细步骤

1 篇文章 0 订阅

1.依赖:

        <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.2</version>
        </dependency>
        <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>
        <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.4</version>
        </dependency>
        <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.2.1.RELEASE</version>
        </dependency>

2.创建对应数据库的实体类

3.创建mapper接口

//mapper接口用于定义,增删改查,用于编写mapper文件
public interface BookMapper {
    //增加一本书
    int addBook(Books books);
    //删除一本书
    int deleteBookById(@Param("bookID")int bookID);
    //修改一本书
    int updateBook(Books books);
    //查询一本书
    Books queryBookById(@Param("bookID")int bookID);
    //查询所有书
    List<Books> queryAllBook();
}

3.创建mybatis的配置文件

<?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.xzb.pojo"/>
    </typeAliases>

    <!--配置数据源,已经交由spring-mapper.xml处理-->

    <!--注册mapper,已经交由spring-mapper.xml处理-->
    <!--<mappers>
        <mapper resource="com/xzb/mapper/BookMapper.xml"/>
    </mappers>-->

</configuration>

4.创建数据库信息的properties文件

# 配置数据库相关信息
jdbc.driver=com.mysql.jdbc.Driver
# &serverTimezone=Asia/Shanghai
jdbc.url=jdbc:mysql://localhost:3306/stu?useSSL=true&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root

5.创建对应实体的spring文件 spring-mapper.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:aop="http://www.springframework.org/schema/aop"
       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/aop
                           http://www.springframework.org/schema/aop/spring-aop.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">

    <!--引入database.properties数据库信息配置文件-->
    <context:property-placeholder location="classpath:database.properties"/>
    <!--配置数据源,可替换其他数据源-->
    <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}"/>
        <!--此下时c3p0数据源特有的配置,可省略-->
        <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>
    <!--配置sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--引入数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--绑定mybatis-config.xml即mybatis核心配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--注册mapper文件-->
        <property name="mapperLocations" value="classpath:com/xzb/mapper/BookMapper.xml"/>
    </bean>
    <!--配置dao/mapper接口类扫描,实现dao/mapper自动注入IOC容器中
            为我我们省略了写mapper实现类通过sqlSessionTemplate.getMapper()执行mapper接口查询数据库的步骤-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--value="sqlSessionFactory"对应上方配置的id-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--指定扫描mapper包中的类,自动给他们分配好了带有return sqlSessionTemplate.getmspper().接口方法 可直接由接口执行数据库操作-->
        <property name="basePackage" value="com.xzb.mapper"/><!--spring-service.xml的bookMapper在此出现-->
    </bean>

    <!--相当于给org.mybatis.spring.SqlSessionTemplate配置SqlSessionTemplate
            并注入到了com.xzb.mapper.impl.PatriarchMapperImpl
            相当于一下操作:-->
    <!--<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">-->
            <!--注入sqlSessionFactory-->
        <!--<constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>-->
    <!--注册实现类-->
    <!--<bean id="patriarchMapper" class="com.xzb.mapper.impl.PatriarchMapperImpl">-->
        <!--给mapper实现了注入sqlSessionTemplate-->
        <!--<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>-->
    <!--</bean>-->

</beans>

6.创建对应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">
<!--namespace用来绑定一个dao/mapper接口-->
<mapper namespace="com.xzb.mapper.BookMapper">
<!--实现mapper接口方法,实现增删改查-->
    <insert id="addBook" parameterType="books">
        insert into books (bookName, bookCounts, detail)
        values (#{bookName},#{bookCounts},#{detail})
    </insert>

    <delete id="deleteBookById" parameterType="int">
        delete from books
        where bookID = #{bookID}
    </delete>

    <update id="updateBook" parameterType="books">
        update books
        set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail}
        where bookID = #{bookID}
    </update>

    <select id="queryBookById" parameterType="int" resultType="books">
        select * from books
        where bookID = #{bookID}
    </select>

    <select id="queryAllBook" resultType="books">
        select * from books
    </select>

</mapper>

7.创建业务

public interface BookService {

    //增加一本书
    int addBook(Books books);
    //删除一本书
    int deleteBookById(int bookID);
    //修改一本书
    int updateBook(Books books);
    //查询一本书
    Books queryBookById(int bookID);
    //查询所有书
    List<Books> queryAllBook();
}
//------------------------------------
//@Service
public class BookServiceImpl implements BookService{
    //@Autowired
    private BookMapper bookMapper;//注入此属性,用于调用bookMapper方法,获取mapper层查询的书籍

    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    @Override
    public int addBook(Books books) {
        return bookMapper.addBook(books);
    }

    @Override
    public int deleteBookById(int bookID) {
        return bookMapper.deleteBookById(bookID);
    }

    @Override
    public int updateBook(Books books) {
        return bookMapper.updateBook(books);
    }

    @Override
    public Books queryBookById(int bookID) {
        return bookMapper.queryBookById(bookID);
    }

    @Override
    public List<Books> queryAllBook() {
        return bookMapper.queryAllBook();
    }
}

8.创建对应service层的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:aop="http://www.springframework.org/schema/aop"
       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/aop
                           http://www.springframework.org/schema/aop/spring-aop.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">

    <!--配置扫描器扫描service包-->
    <context:component-scan base-package="com.xzb.service"/>
    <!--配置业务类-->
    <!--配置BookService业务的实现类,别给其bookMapper属性赋值-->
    <bean id="bookServiceImpl" class="com.xzb.service.impl.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>
    <!--配置spring事务声明式事务,管理器-->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          id="transactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--AOP切入事务,支持-->

</beans>

9.创建对应mvc的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:aop="http://www.springframework.org/schema/aop"
       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/aop
                           http://www.springframework.org/schema/aop/spring-aop.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包-->
    <context:component-scan base-package="com.xzb.controller"/>
    <!--配置静态资源不过滤-->
    <mvc:default-servlet-handler/>
    <!--配置处理器驱动 相当于配置了处理器映射器,处理器适配器-->
    <mvc:annotation-driven/>
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

10.创建总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:aop="http://www.springframework.org/schema/aop"
       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/aop
                           http://www.springframework.org/schema/aop/spring-aop.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">

    <!--导入三层spring配置文件-->
    <import resource="classpath:spring-mapper.xml"/>
    <import resource="classpath:spring-service.xml"/>
    <import resource="classpath:spring-mvc.xml"/>

</beans>

11.更改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">

    <!--配置spring内置的DispatcherServlet前端控制/分发器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!--注意此处应用总的 soring配置文件,不然获取不到配置的bean,导致500错误-->
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--配置springmvc编码过滤器-->
        <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>


    <!--配置15分钟无操作,则刷新session-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>

12.创建controller

//控制层 控制页面跳转
@Controller
@RequestMapping("/book")//指明所有有效请求以/book打头
//@CrossOrigin //支持跨域请求
public class BookController {

    @Autowired//spring自动注入
    @Qualifier("bookServiceImpl")//指定注入属性的id,防止同类型同名,无法分辨注入
    private BookService bookService;

    @RequestMapping("/allBook")//设置访问请求
    public String list(Model model){

        List<Books> list = bookService.queryAllBook();//调用业务

        model.addAttribute("list",list);//封装数据
        model.addAttribute("log","执行查询");

        return "allBook";//设置页面
    }

    @RequestMapping("/toaddBook")//跳转到添加页面
    public String toAdd(){
        return "addBook";
    }

    @RequestMapping("/addBook")//添加书籍
    public String addBook(Books books){

        bookService.addBook(books);//执行添加的业务

        return "redirect:/book/allBook";//添加成功和跳转到查询全部的页面
    }

    @RequestMapping("/toUpdateBook")//携带修改目标跳转
    public String toUpdate(int id,Model model){

        Books books = bookService.queryBookById(id);

        model.addAttribute("books",books);

        return "updateBook";
    }

    @RequestMapping("/updateBook")//执行修改
    public String updateBook(Books books){

        bookService.updateBook(books);

        return "redirect:/book/allBook";//修改成功和跳转到查询全部的页面
    }

    @RequestMapping("/deleteBook")//执行删除
    public String deleteBook(int id){

        bookService.deleteBookById(id);

        return "redirect:/book/allBook";//删除成功和跳转到查询全部的页面
    }

    @RequestMapping("queryByid")
    public String queryByid(int id,Model model){

        Books books = bookService.queryBookById(id);

        List<Books> list = new ArrayList<Books>();

        if (books == null){
            list = bookService.queryAllBook();
            model.addAttribute("log","查询失败");
        }else {
            list.add(books);
            model.addAttribute("log","查询成功");
        }

        model.addAttribute("list",list);

        return "allBook";
    }

}

/**
 *      @Autowired//自动装配IOC容器中 存在的 (根据id(autowire="byType"-再->autowire="byName" )注入)
 *     //作用于属性也可以加到set方法上
 *     //required = false属性默认为true 表示不能为空 false表示可为空
 *     @Qualifier(value = "cars")//当bean中有多个相同type时  配和Qualifier使用 指定bean的id
 *     private Car car;//同时也可使用Java原生注解@Resource(name=""),type=""实现自动装配,
 *                     // 但不如spring注解便利(type->name)
 */

13.在WEB-INF下创建jsp文件夹并将页面放于此文件夹中

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值