首页 > 代码库 > ssm框架搭建(上)

ssm框架搭建(上)

前言

之前也说过,工作做的开发都是基于公司现有的框架,心里很没底。所以一直想自己能够搭建出ssm框架。经过多次尝试,终于成功了。这边文章将从两个方面进行,一是框架搭建,二是简单的增删查改。

正文

1.环境搭建

这里采用现在流行的maven方式,而是将需要的jar放在web_inf\lib下面了。

技术分享

直接贴了一张图,有点任性了...整个工程的结构如下图所示

技术分享

在conf子包中,是配置文件。mapper下对应mybatis的映射文件,里面包含了相应的sql语句。(mvcLearn\conf\mapper\UserMapper.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">
<!-- 文件的命名 表名 + Mapper -->

<!-- namespace:接口包 + 接口名
    
 -->
<mapper namespace="org.tonny.map.IUserMapper">
    <select id="getUserById" parameterType="int" resultType="org.tonny.model.User">
        SELECT *
        FROM users WHERE id=#{id}
    </select>
    
    <select id="getAll" resultType="org.tonny.model.User">
        SELECT *
        FROM users
    </select>
    
    <insert id="add" parameterType="org.tonny.model.User">
        INSERT INTO users(name,age)
        VALUES(#{name}, #{age})
    </insert>
    
    <update id="update" parameterType="org.tonny.model.User">
        UPDATE users
        SET name = #{name},age = #{age}
        WHERE id=#{id}
    </update>
    
    <delete id="delete" parameterType="int">
        DELETE FROM users
        WHERE id=#{id}
    </delete>
</mapper>

db.properties是数据库相关的信息,如ip,用户名,密码等。

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=1qaz2wsx

 

log4j.properties是log日志的配置文件,记录了输出级别,输出形式等。

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

 

mybatis.xml是mybatis的配置文件,里面描述了mybatis的配置信息,如别名,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>
    <!-- 设置日志打印 -->
    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>
    
    <!-- 配置映射类的别名 -->
    <typeAliases>
        <typeAlias alias="User" type="org.tonny.model.User" />
    </typeAliases>
    <!-- 配置Mapper文件的路径 -->
    <mappers>
        <!-- <mapper resource="mapper/UserMapper.xml" /> -->
    </mappers>
</configuration>

 

spring-mvc.xml是springmvc的配置文件,里面配置了如分发器,jsp前缀后缀等。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="  
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-4.1.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans       
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-4.1.xsd">


    <mvc:annotation-driven />

    <!-- 把标记了@Controller注解的类转换为bean,这个里面只要关注控制器,其他的bean在spring中去扫描 -->
    <context:component-scan base-package="org.tonny.controller" />

    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/view/" p:suffix=".jsp" />


</beans>

 

spring.xml则描述了bean的配置,一些配置文件之间的关系则会在这里体现。

<?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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="  
           http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
           http://www.springframework.org/schema/aop  
           http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    
    <!-- 自动扫描注解的bean -->
    <context:component-scan base-package="org.tonny" />
    
    <!-- 引入配置文件 -->    
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
        <property name="locations">    
            <list>    
                <value>classpath*:db.properties</value>
            </list>    
        </property>    
    </bean>    
    
    <!-- 配置数据源 -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driverClassName}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    </bean>

    <!-- 引用Mapper文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 设置mybatis文件 -->
        <property name="configLocation" value="classpath:mybatis.xml"></property>
        
        <!-- 扫描mybatis表对应的文件 -->
        <property name="mapperLocations" value="classpath*:mapper/*Mapper.xml" />
    </bean>

    <!-- 配置接口与mapper.xml的对应关系 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName"  value="sqlSessionFactory"/>
        <!-- 
        如果使用
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        会发生错误,因为db.propertites加载在sqlSessionFactory之后,所以使用sqlSessionFactoryBeanName代替
         -->
        <!-- 配置mapper对应的接口,指定所在的包 -->
        <property name="basePackage" value="org.tonny.map" />
    </bean>
</beans>

 

最后是web.xml中汇总,形成关联。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    <!-- Spring 容器加载 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>
    
    <!-- log4j 这一段不配置,log4j配置文件也是可以使用的,这边应该单独给Spring的使用-->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>600000</param-value>
    </context-param>
    
    <!-- SpringMVC的前端控制器 -->
    <servlet>
        <servlet-name>spring-dispatcher</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>
        <!-- 何时启动 大于0的值表示容器启动时初始化此servlet,正值越小优先级越高 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- Spring MVC配置文件结束 -->

    <!-- SpringMVC拦截设置 -->
    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <!-- 由SpringMVC拦截所有请求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- SpringMVC拦截设置结束 -->

    <!--解决中文乱码问题 -->
    <filter>
        <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

 

ssm框架搭建(上)