SSM框架

新建maven项目
1.pom.xml中加依赖

<!--依赖,junit,数据库驱动,连接池,servlet,jsp,mybatis,mybatis-spring,spring-->
<dependencies>
<!--junit-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
<!--数据库驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.20</version>
    </dependency>
<!--数据库连接池-->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.2</version>
    </dependency>
    <!--servlet-JSP-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.2.1</version>
        <scope>provided</scope>
    </dependency>
<!--mybatis-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.2</version>
    </dependency>
<!--mybatis-spring-->
    <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.1.9.RELEASE</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>

右边maven点击刷新
2.连接数据库
右边database->加号->mysql->输入用户名和密码(记得在Advanced中设置serverTimezone的驱动属性,value= Asia/Shanghai)->TestConnection
3.main->java下新建包
pojo、dao、service、controller
main->resource下新建File
mybatis-config.xml(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>

</configuration>

applicationContext.xml(Spring的核心配置文件)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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 ">

</beans>

有一个黄色的config,点击创建一个
4.mybatis层编写
连接数据库,main->resource下新建database.properties

jdbc.driver=com.mysql.jdbc.driver
# mysql8.0+要加&serverTimezone=Asia/Shanghai
jdbc.url=jdbc:mysql://localhost:3306/book?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456

mybatis-config.xml

<configuration>
<!--配置数据源,交给spring去做-->
<!--给包起别名-->
    <typeAliases>
        <package name="com.qmz.pojo"/>
    </typeAliases>

</configuration>

pojo下新建类Books
dao下新建接口BookMapper、
File 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.qmz.dao.BookMapper">
    <insert id="addBook" parameterType="Books">
        insert into book.books(bookname,bookcunt,detail)
        values (#{bookname},#{bookcunt},#{detail});
    </insert>
    <delete id="deleteBookById" parameterType="int">
        delete from book.books where bookid=#{bookid};
    </delete>
    <update id="updateBook" parameterType="Books">
        update book.books set bookname=#{bookname},bookcunt=#{bookcunt},detail=#{detail}
         where bookid=#{bookid};
    </update>
    <select id="queryBookById" resultType="Books">
         select * from  book.books  where bookid=#{bookid};
    </select>
    <select id="queryAllBook">
        select * from book.books;
    </select>
</mapper>

mybatis-config.xml中配置映射mapper

 <mappers>
        <mapper class="com.qmz.dao.BookMapper"></mapper>
    </mappers>

service下建BookService接口、BookServiceImpl类
5.Spring层编写
main->resource下新建Spring-dao.xml

和上面applicationContext.xml(Spring的核心配置文件)内容一样
<?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">
<!--1.关联数据库配置文件:(固定的)-->
    <context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
<!--2.连接池
 dbcp :手动化操作,不能自动连接
 c3p0:自动化操作(自动化加载配置文件,并且可以自动设置到对象中!)
 druid
 hikari
 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>

    </bean>
<!--3.sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--绑定Mybatis的配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>

<!--配置dao接口扫描包,动态地实现了dao接口自动注入到spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!--要扫描的dao包-->
        <property name="basePackage" value="com.qmz.dao"></property>

    </bean>

</beans>

main->resource下新建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">
<!--1.扫描service下的包-->
    <context:component-scan base-package="com.qmz.service"></context:component-scan>
<!--2.将我们所有的业务类,注入到Spring,可以通过配置或者注解来实现-->
    <bean id="BookServiceImpl" class="com.qmz.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"></property>
    </bean>
<!--3.声明式事务配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--    注入数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--4.横切事务AOP-->




</beans>

6.Spring MVC层编写
a.先把项目添加为web项目(右击项目名->Add FrameWork Support->勾选web)
b.resource下新建spring-mvc.xml(头和applicationContext.xml一样)
c.在applicationContext.xml中配置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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd ">

<import resource="classpath:Spring-dao.xml"></import>
<import resource="classpath:Spring-service.xml"></import>
<import resource="classpath:spring-mvc.xml"></import>
</beans>

d.在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">
<!--  DispatchServlet  -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
<!--        和tomcat一起启动-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
<!--    乱码过滤-->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

<!--    Session-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>

</web-app>

e.在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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--1.注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>
<!--2.静态资源过滤-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--3.扫描包:controller-->
    <context:component-scan base-package="com.qmz.controller"></context:component-scan>
<!--4.视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

f.在WEB-INF下 new Diroctory (jsp)

-------------------------------------------框架整合结束-------------------------------------------

以下只需要将controller层和jsp层交互起来就可以了。
在controller包下新建BookController类

@Controller
@RequestMapping("/book")
public class BookController {
    //Controller层调Service层
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    //查询全部书籍,并返回到一个书籍展示页面
    @RequestMapping("/allBook")
    public String List(Model model) {
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list",list);
        return "allBook";
    }
}

在jsp下新建allBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>书籍展示页面</title>
</head>
<body>
<h3>
    <a href="${pageContext.request.contextPath}/book/allBook"></a>
</h3>
</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值