SSM框架

我是用maven敲的

首先引用pom.xml里面的依赖

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

    <groupId>com.fhx</groupId>
    <artifactId>SSM_1.17</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <!-- springMvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <!-- servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>
        <!-- mybats -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!-- jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <!-- jstl-->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

    <!--tomcat 插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!-- 指定端口 -->
                    <port>8081</port>
                    <!-- 请求路径 -->
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

这个是结构
在这里插入图片描述
在WEB-INF里面写web.xml

在里面分别写 监听 servlet 上下文

web.xml里面的上下文

指定resources里面config里面的配置文件

<?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_3_1.xsd"
         version="3.1">
    <!-- 监听 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- servlet -->
    <servlet>
        <servlet-name>SSM_117</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SSM_117</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


    <!--上下文-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:config/spring-*.xml
            classpath*:config/applicationContext.xml
        </param-value>
    </context-param>
</web-app>

项目进去之后

首先会访问web.xml

根据servlet里面的 的名字

创建

SSM_117-servlet.xml

这个里面还有视图解析器 直接指向路径jsp

<?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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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">
    <!-- 开启注解 -->
    <mvc:annotation-driven/>
    <!-- 指定controller指定路径  扫描其注解 -->
    <context:component-scan base-package="com.fhx.controller"/>

    <!-- 视图解析 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:viewClass="org.springframework.web.servlet.view.JstlView"
          p:prefix="/jsp/"
          p:suffix=".jsp"/>

</beans>

WEB-INF里面的2个写完了

现在开始写resources里面的文件

web.xml上下文直接指定applicationContext.xml

resources里面有
com/fhx/mapper-----DemoMapper.xml
config ------- applicationContext.xml (可以加载资源文件 指定service)
-------spring-mybatis.xml(指定mapper 实体类sesstion工厂)
-------spring-mysql.xml
mapper
------mybatis.cfg.xml
properties
-------mysql.properties(mysql 连接数据库的一些资源文件)

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 http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 扫描service -->
    <context:component-scan base-package="com.fhx.service"/>
    <context:annotation-config/>
    <!--  加载资源文件 properties -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>
                    classpath*:properties/mysql.properties
                </value>
            </list>
        </property>
        <property name="fileEncoding" value="UTF-8"/>
    </bean>
</beans>

mysql.properties

mysql 连接数据库的资源文件

mysql.url=jdbc:mysql://localhost:3306/linux
mysql.driverClassName=com.mysql.jdbc.Driver
mysql.user=root
mysql.password=root

spring-mybatis.xml 创建session工厂

对象实体类 mapper

<?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">
    <!-- 配置Session工厂 -->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--configLocation属性指定mybatis的核心配置文件 -->
        <property name="configLocation" value="classpath:mapper/mybatis.cfg.xml"/>
        <property name="mapperLocations" value="classpath:com/fhx/mapper/*Mapper.xml"/>
        <!-- 自动扫描需要定义类别名的包,将包内的JAVA类的类名作为类别名 -->
        <property name="typeAliasesPackage" value="com.fhx.entity"/>
    </bean>
    <!-- 自动扫描所有的Mapper接口与文件 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.fhx.mapper"/>
    </bean>

</beans>

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

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="url" value="${mysql.url}"/>
    <property name="username" value="${mysql.user}"/>
    <property name="password" value="${mysql.password}"/>
    <property name="driverClassName" value="${mysql.driverClassName}"/>
</bean>
</beans>

mapper里面的这个 mybatis.cfg.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>

DemoMapper.xml

写sql语句的

<?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" >
<mapper namespace="com.fhx.mapper.DemoMapper">

    <select id="list" resultType="com.fhx.entity.Clazz">
        select
        *
        from clazz
    </select>
</mapper>

最后这些配置应该完了

可以敲层了

controller------service-----servicelmpl-----mapper----mapper.xml



加入乱码了

可以再访问路径的时候加上这个东西

   @RequestMapping(value = "/demo",produces = {"application/json;charset=utf-8"})
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值