码迷,mamicode.com
首页 > 其他好文 > 详细

SSM 整合配置以及一个简单登陆案例(个人记录)

时间:2019-03-08 23:52:47      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:命名   eal   def   个人   hid   stl   数据绑定   get   --   

SSM 文件以及大部分参考博客 https://blog.csdn.net/qq598535550/article/details/51703190 

简答的登陆注册案例下载链接在末尾补贴图了

我建立的是 IDEA maven 项目 pomxml 文件如下

技术图片
  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5   <modelVersion>4.0.0</modelVersion>
  6 
  7   <groupId>com</groupId>
  8   <artifactId>SSMLoginEnd</artifactId>
  9   <version>1.0-SNAPSHOT</version>
 10   <packaging>war</packaging>
 11 
 12   <name>SSMLoginEnd Maven Webapp</name>
 13   <!-- FIXME change it to the projects website -->
 14   <url>http://www.example.com</url>
 15 
 16   <properties>
 17     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 18     <maven.compiler.source>1.7</maven.compiler.source>
 19     <maven.compiler.target>1.7</maven.compiler.target>
 20   </properties>
 21 
 22   <dependencies>
 23     <!-- 单元测试 -->
 24     <dependency>
 25       <groupId>junit</groupId>
 26       <artifactId>junit</artifactId>
 27       <version>4.12</version>
 28     </dependency>
 29 
 30     <!-- 1.日志 -->
 31     <!-- 实现slf4j接口并整合 -->
 32     <dependency>
 33       <groupId>ch.qos.logback</groupId>
 34       <artifactId>logback-classic</artifactId>
 35       <version>1.1.1</version>
 36     </dependency>
 37 
 38     <!-- 2.数据库 -->
 39     <dependency>
 40       <groupId>mysql</groupId>
 41       <artifactId>mysql-connector-java</artifactId>
 42       <version>5.1.37</version>
 43       <scope>runtime</scope>
 44     </dependency>
 45     <dependency>
 46       <groupId>c3p0</groupId>
 47       <artifactId>c3p0</artifactId>
 48       <version>0.9.1.2</version>
 49     </dependency>
 50 
 51     <!-- DAO: MyBatis -->
 52     <dependency>
 53       <groupId>org.mybatis</groupId>
 54       <artifactId>mybatis</artifactId>
 55       <version>3.3.0</version>
 56     </dependency>
 57     <dependency>
 58       <groupId>org.mybatis</groupId>
 59       <artifactId>mybatis-spring</artifactId>
 60       <version>1.2.3</version>
 61     </dependency>
 62 
 63     <!-- 3.Servlet web -->
 64     <dependency>
 65       <groupId>taglibs</groupId>
 66       <artifactId>standard</artifactId>
 67       <version>1.1.2</version>
 68     </dependency>
 69     <dependency>
 70       <groupId>jstl</groupId>
 71       <artifactId>jstl</artifactId>
 72       <version>1.2</version>
 73     </dependency>
 74     <dependency>
 75       <groupId>com.fasterxml.jackson.core</groupId>
 76       <artifactId>jackson-databind</artifactId>
 77       <version>2.5.4</version>
 78     </dependency>
 79     <dependency>
 80       <groupId>javax.servlet</groupId>
 81       <artifactId>javax.servlet-api</artifactId>
 82       <version>3.1.0</version>
 83     </dependency>
 84 
 85     <!-- 4.Spring -->
 86     <!-- 1)Spring核心 -->
 87     <dependency>
 88       <groupId>org.springframework</groupId>
 89       <artifactId>spring-core</artifactId>
 90       <version>5.1.5.RELEASE</version>
 91     </dependency>
 92     <dependency>
 93       <groupId>org.springframework</groupId>
 94       <artifactId>spring-beans</artifactId>
 95       <version>5.1.5.RELEASE</version>
 96     </dependency>
 97     <dependency>
 98       <groupId>org.springframework</groupId>
 99       <artifactId>spring-context</artifactId>
100       <version>5.1.5.RELEASE</version>
101     </dependency>
102     <!-- 2)Spring DAO层 -->
103     <dependency>
104       <groupId>org.springframework</groupId>
105       <artifactId>spring-jdbc</artifactId>
106       <version>5.1.5.RELEASE</version>
107     </dependency>
108     <dependency>
109       <groupId>org.springframework</groupId>
110       <artifactId>spring-tx</artifactId>
111       <version>5.1.5.RELEASE</version>
112     </dependency>
113     <!-- 3)Spring web -->
114     <dependency>
115       <groupId>org.springframework</groupId>
116       <artifactId>spring-web</artifactId>
117       <version>5.1.5.RELEASE</version>
118     </dependency>
119     <dependency>
120       <groupId>org.springframework</groupId>
121       <artifactId>spring-webmvc</artifactId>
122       <version>5.1.5.RELEASE</version>
123     </dependency>
124     <!-- 4)Spring test -->
125     <dependency>
126       <groupId>org.springframework</groupId>
127       <artifactId>spring-test</artifactId>
128       <version>5.1.5.RELEASE</version>
129     </dependency>
130 
131     <!-- redis客户端:Jedis -->
132     <dependency>
133       <groupId>redis.clients</groupId>
134       <artifactId>jedis</artifactId>
135       <version>2.7.3</version>
136     </dependency>
137     <dependency>
138       <groupId>com.dyuproject.protostuff</groupId>
139       <artifactId>protostuff-core</artifactId>
140       <version>1.0.8</version>
141     </dependency>
142     <dependency>
143       <groupId>com.dyuproject.protostuff</groupId>
144       <artifactId>protostuff-runtime</artifactId>
145       <version>1.0.8</version>
146     </dependency>
147 
148     <!-- Map工具类 -->
149     <dependency>
150       <groupId>commons-collections</groupId>
151       <artifactId>commons-collections</artifactId>
152       <version>3.2</version>
153     </dependency>
154   </dependencies>
155   <build>
156     <finalName>ssm</finalName>
157   </build>
158 </project>
View Code

在main文件夹下建立resource 文件夹 并设置为 把resource资源文件夹 目录如下

 

技术图片

mapper 下储存你 写为sql 语句xml 

spring 下的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
    http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置整合mybatis过程 -->
    <!-- 1.配置数据库相关参数properties的属性:${url} -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!-- 2.数据库连接池 -->
    <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>

    <!-- 3.配置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 使用别名 -->
        <property name="typeAliasesPackage" value="bean" />
        <!-- 扫描sql配置文件:mapper需要的xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>

    <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <!-- 给出需要扫描mapper接口包  -->
        <property name="basePackage" value="mapper" />
    </bean>
</beans>
View Code

spring 下的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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 扫描service包下所有使用注解的类型 -->
    <context:component-scan base-package="service" />

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

    <!-- 配置基于注解的声明式事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>
View Code

spring 下的spring-web

技术图片
<?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: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/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- 配置SpringMVC -->
    <!-- 1.开启SpringMVC注解模式 -->
    <!-- 简化配置:
        (1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter
        (2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持
    -->
    <mvc:annotation-driven />

    <!-- 2.静态资源默认servlet配置
        (1)加入对静态资源的处理:js,gif,png
        (2)允许使用"/"做整体映射
     -->
    <mvc:default-servlet-handler/>

    <!-- 3.配置jsp 显示ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 4.扫描web相关的controller -->
    <context:component-scan base-package="controller" />
</beans>
View Code

jdbc.properties

技术图片
jdbc.driver=com.mysql.jdbc.Driver
#数据库地址
jdbc.url=jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=utf8
#用户名
jdbc.username=root
#密码
jdbc.password=root
View Code

logback.xml

技术图片
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are by default assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>
View Code

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>
        <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
        <setting name="useGeneratedKeys" value="true" />

        <!-- 使用列别名替换列名 默认:true -->
        <setting name="useColumnLabel" value="true" />

        <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>
View Code

web.xml

技术图片
<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" metadata-complete="true">
  <!-- 如果是用mvn命令生成的xml,需要修改servlet版本为3.1 -->
  <!-- 配置DispatcherServlet -->
  <welcome-file-list>
    <welcome-file>portal/index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>portal/index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>seckill-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 配置springMVC需要加载的配置文件
        spring-dao.xml,spring-service.xml,spring-web.xml
        Mybatis - > spring -> springmvc
     -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-*.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>seckill-dispatcher</servlet-name>
    <!-- 默认匹配所有的请求 -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
View Code

测试基类 测试的时候继承这个类 加个@Test就行

package test;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
// 告诉junit spring配置文件
@ContextConfiguration({ "classpath:spring/spring-dao.xml", "classpath:spring/spring-service.xml" })
public class BaseTest {
}

 

 

遇到的问题(刚学也没系统的看教程)

1 里面配置不太清楚 准备日后理解 这是一个模板

2 三层关系要搞清楚

JAVAEE 开发三层

Controller 和页面进行数据交互
Service Controller 调用Service ,进行业务逻辑操作 Service 调用mapper 查询数据操作
mapper ,dao 和数据库进行数据交hu

3 进行  数据库参数大于1 的操作时要加 @parm 

例如

boolean insertUser(@Param("username")String username, @Param("password")String password);
具体参照 https://blog.csdn.net/weixin_37139197/article/details/82975594

源代码 下载地址
https://www.lanzous.com/i3d5vgd

 

SSM 整合配置以及一个简单登陆案例(个人记录)

标签:命名   eal   def   个人   hid   stl   数据绑定   get   --   

原文地址:https://www.cnblogs.com/xuexidememeda/p/10498677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!