zoukankan      html  css  js  c++  java
  • 整合SSM框架

    1.创建数据库

    SET FOREIGN_KEY_CHECKS=0;

    -- ----------------------------
    -- Table structure for test_entity
    -- ----------------------------
    DROP TABLE IF EXISTS `test_entity`;
    CREATE TABLE `test_entity` (
    `id` varchar(40) NOT NULL COMMENT '主键 UUID',
    `testName` varchar(40) DEFAULT NULL COMMENT '测试testName',
    `testInt` int(100) DEFAULT NULL COMMENT '测试testInt',
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    -- ----------------------------
    -- Records of test_entity
    -- ----------------------------
    INSERT INTO `test_entity` VALUES ('0250f7f9-bb82-48c6-89b7-ce59d5ff4d19', '测试1', '1');
    INSERT INTO `test_entity` VALUES ('1a557452-9d92-4188-9036-ddafc205dced', '测试2', '2');
    INSERT INTO `test_entity` VALUES ('234411fa-a9a5-48f5-aa23-df75a5ee6d4c', '测试3', '3');
    INSERT INTO `test_entity` VALUES ('256f2947-e4a3-4031-bb7e-b822da83d536', '测试4', '4');
    INSERT INTO `test_entity` VALUES ('2dd0458a-5577-4cb0-9a46-de1cb6e73fb0', '测试5', '5');
    INSERT INTO `test_entity` VALUES ('32975f28-e9f9-4738-b6b5-90d900168264', '测试6', '6');
    INSERT INTO `test_entity` VALUES ('39911075-34a8-433f-85fb-4dcbee7eb744', '测试7', '7');
    INSERT INTO `test_entity` VALUES ('4087d303-ee56-4911-b4de-25fb93985bab', '测试8', '8');
    INSERT INTO `test_entity` VALUES ('7dc52b40-fa65-4ff8-b6d7-92fe3bdbc325', '测试9', '9');
    INSERT INTO `test_entity` VALUES ('8c88fa9f-c450-47fb-ad5e-7e94812c4430', '测试10', '10');
    INSERT INTO `test_entity` VALUES ('b69011b3-ebc6-4135-9984-834bd95e2076', '测试11', '11');
    INSERT INTO `test_entity` VALUES ('g69011b3-ebc6-4135-9984-834bd67e2076', '测试12', '12');

    2.创建Maven项目

     

    然后一直下一步下一步直至完成

    在main下创建java包和resources包

    • 选择创建的java右键Mark Directory as->Sources Root
    • 选择创建的resources右键Mark Directory as->Test Sources Root

    在java下创建包结构

    3.配置

    3.1依赖加载

    <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.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.1.9.RELEASE</version>
    </dependency>

    <!--lombok-->
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.10</version>
    </dependency>
    <!--fastjson-->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.62</version>
    </dependency>
    <!--log4j-->
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    </dependency>
    </dependencies>

    3.2Maven资源过滤设置

    <build>
        <!--Maven资源过滤设置-->
        <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>

    4.在resources下创建相应配置文件

    database.properties

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

    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>

    <settings>
    <!-- 开启日志,使用log4j日志格式-->
    <setting name="logImpl" value="LOG4J"/>
    <!-- 显示开启二级缓存-->
    <setting name="cacheEnabled" value="true"/>
    </settings>

    <!--自动添加前置包名-->
    <typeAliases>
    <package name="com.ssm.entity"/>
    </typeAliases>

    <!-- 绑定映射 -->
    <mappers>
    <!-- 将包内的映射器接口实现全部注册为映射器 -->
    <package name="com.ssm.dao"/>
    </mappers>
    </configuration>

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

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

    log4j.properties

    # 将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
    log4j.rootLogger=DEBUG,console
    
    # 控制台输出的相关设置
    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.console.Target=System.out
    log4j.appender.console.Threshold=DEBUG
    log4j.appender.console.layout=org.apache.log4j.PatternLayout
    log4j.appender.console.layout.ConversionPattern=[%c]--%m%n
    
    # 文件输出的相关设置
    #log4j.appender.file = org.apache.log4j.RollingFileAppender
    #log4j.appender.file.File=./log/log.log
    #log4j.appender.file.MaxFileSize=10mb
    #log4j.appender.file.Threshold=DEBUG
    #log4j.appender.file.layout=org.apache.log4j.PatternLayout
    #log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd HH:mm:ss}][%c]%m%n
    
    # 日志输出级别
    log4j.logger.org.mybatis=DEBUG
    log4j.logger.java.sql=DEBUG
    log4j.logger.java.sql.Statement=DEBUG
    log4j.logger.java.sql.ResultSet=DEBUG
    log4j.logger.java.sql.PreparedStatement=DEBUG

    5.xml配置

    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 -->
    <!-- 关联数据库文件 -->
    <context:property-placeholder location="classpath:database.properties"/>

    <!--数据库连接池:
    dbcp 半自动化操作 不能自动连接
    c3p0 自动化操作(自动的加载配置文件,并且设置到对象里面)
    -->
    <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"/>
    <!-- 关闭连接后不自动commit -->
    <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"/>
    <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!-- 配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
    <!-- 解释:https://www.cnblogs.com/jpfss/p/7799806.html -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 注入sqlSessionFactory -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    <!-- 给出需要扫描Dao接口包 -->
    <property name="basePackage" value="com.ssm.dao"/>
    </bean>
    </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: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/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 配置SpringMVC -->
    <!-- 开启SpringMVC注解驱动 -->
    <mvc:annotation-driven>
    <!--防止返回页面出现中文乱码-->
    <mvc:message-converters register-defaults="true">
    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
    <constructor-arg value="UTF-8" />
    </bean>
    </mvc:message-converters>
    </mvc:annotation-driven>
    <!-- 静态资源默认配置-->
    <mvc:default-servlet-handler/>

    <!-- 配置jsp 显示ViewResolver视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
    </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
    http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 扫描相关的bean -->
    <context:component-scan base-package="com.ssm" />

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!-- 注入数据库连接池 -->
    <property name="dataSource" ref="dataSource" />
    </bean>

    </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>
    <!-- / 匹配所有的请求(不包括.jsp)-->
    <!-- /* 匹配所有的请求(包括.jsp)-->
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--encodingFilter-->
    <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>
    </web-app>

    配置文件配置完成后的目录

    6.实现业务类

    在controller包下创建TestController类

    @RestController
    public class TestController {

    @Autowired
    private TestEntityService testEntityService;

    @RequestMapping(value = "/test01", method = RequestMethod.GET)
    public String test01() {
    return JSON.toJSONString(testEntityService.listTestEntitys());
    }

    @RequestMapping(value = "/test02", method = RequestMethod.GET)
    public String test02(@RequestParam("id") String id) {
    return JSON.toJSONString(testEntityService.getTestEntity(id));
    }

    }

    在dao包下创建TestEntityMapper类和TestEntityMapper.xml

    TestEntityMapper类

    public interface TestEntityMapper {

    /**
    * 查询TestEntity集合
    */
    List<TestEntity> listTestEntitys();

    /**
    * 根据id精确查询TestEntity
    */
    TestEntity getTestEntity(String id);
    }

    TestEntityMapper.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="com.ssm.dao.TestEntityMapper">

    <!--查询TestEntity集合-->
    <select id="listTestEntitys" resultType="TestEntity">
    SELECT id,testName,testInt from test_entity
    </select>

    <!--根据id精确查询TestEntity-->
    <select id="getTestEntity" resultType="TestEntity">
    SELECT id,testName,testInt from test_entity where id = #{id}
    </select>
    </mapper>

    在entity包下创建实体类TestEntity

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Accessors(chain = true)
    public class TestEntity {

    private String id;

    private String testName;

    private Integer testInt;
    }

    在service包下创建TestEntityServices类和impl包

    public interface TestEntityService {

    List<TestEntity> listTestEntitys();

    TestEntity getTestEntity(String id);
    }

    在impl包下创建TestEntityserviceImpl类

    @Service
    public class TestEntityServiceImpl implements TestEntityService {

    @Autowired
    private TestEntityMapper testEntityMapper;

    @Override
    public List<TestEntity> listTestEntitys() {
    return testEntityMapper.listTestEntitys();
    }

    @Override
    public TestEntity getTestEntity(String id) {
    return testEntityMapper.getTestEntity(id);
    }
    }

    业务类完成后目录如下

    然后配置Tomcat启动运行就可以了,在地址栏测试了~

    gitee:https://gitee.com/niugit_admin/ssm

    原文链接:https://blog.kuangstudy.com/index.php/archives/487/

  • 相关阅读:
    [转]以安装桌面体验功能为例来探索windows2012服务器管理器的新变化
    [转]DPM2012系列之十六:在SCOM2012上集成DPM2012中央控制台
    [转]DPM2012系列之十二:还原exchange2010用户邮件
    Windows Phone Dev Center How to deploy and run a Windows Phone app
    [转].NET StockTrader 6 Sample Application
    [转]DPM2012系列之一:安装Data Protection Manager 2012
    [转]DPM2012系列之二十:保护Windows server 2012
    [转]DPM2012系列之十七:如何将备份文件恢复到网络共享文件夹
    [转]DPM2012系列之十四:备份SQL server 2008R2数据库
    [转]使用SCOM 2012监控网络
  • 原文地址:https://www.cnblogs.com/niudaben/p/11969966.html
Copyright © 2011-2022 走看看