<p>用SSM框架已经有一段时间了,但都没有完整地搭建过一次工程。前段时间比较有时间就自己试着搭建了一下,差不多都是用的最新的spring版本了,然后就在这个基础上做了很多的实验测试。最近想着还是记录一下整个搭建的过程,以便于自己日后参考。所以就重新翻新了一下,为了写得尽可能地详细点所以内容有点多。</p>
GitHub的源码地址:SSMDemo GitHub源码
我是用的idea这个软件搭建的,我也一直使用的这个软件,非常好用,很适合做Java的朋友。
1.创建一个maven的web工程。
为了方便我尽可能地上截图了。
创建maven的web工程
项目名称
配置maven
上面的操作就完成了创建工程的基本操作了。
2.创建工程的包结构
创建好各层的测试类和常规的配置文件
整个工程的包结构就创建完成了。
3.导包,这是非常重要的一步,需要将我们使用的spring、springMVC和MyBatis的相关包都导进来,直接使用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.viking.SSDemo
</groupId>
-
<artifactId>SSMDemo
</artifactId>
-
<version>1.0-SNAPSHOT
</version>
-
<packaging>war
</packaging>
-
-
<name>SSMDemo Maven Webapp
</name>
-
<!-- FIXME change it to the project's website -->
-
<url>http://www.example.com
</url>
-
-
<properties>
-
<!--spring版本号,5.0.0RELEASE是2017年9月发布的新版本-->
-
<spring.version>5.0.0.RELEASE
</spring.version>
-
<!--mybatis版本号,3.4.5是2017年8月发布的新版本-->
-
<mybatis.version>3.4.5
</mybatis.version>
-
<!--<slf4j.version>1.7.25</slf4j.version>-->
-
<slf4j.version>1.7.25
</slf4j.version>
-
<log4j.version>1.2.17
</log4j.version>
-
</properties>
-
-
<dependencies>
-
<dependency>
-
<groupId>junit
</groupId>
-
<artifactId>junit
</artifactId>
-
<version>4.11
</version>
-
<scope>test
</scope>
-
</dependency>
-
-
<!--spring核心包——Start-->
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-core
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-web
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-oxm
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-tx
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-jdbc
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-webmvc
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-aop
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-context-support
</artifactId>
-
<version>${spring.version}
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework
</groupId>
-
<artifactId>spring-test
</artifactId>
-
<version>${spring.version}
</version>
-
<scope>test
</scope>
-
</dependency>
-
<!--spring核心包——End-->
-
<!--MyBatis核心包-->
-
<dependency>
-
<groupId>org.mybatis
</groupId>
-
<artifactId>mybatis
</artifactId>
-
<version>${mybatis.version}
</version>
-
</dependency>
-
<dependency>
-
<groupId>com.github.pagehelper
</groupId>
-
<artifactId>pagehelper
</artifactId>
-
<version>4.1.6
</version>
-
</dependency>
-
<dependency>
-
<groupId>tk.mybatis
</groupId>
-
<artifactId>mapper
</artifactId>
-
<version>3.3.8
</version>
-
</dependency>
-
<!--mybatis/spring包-->
-
<dependency>
-
<groupId>org.mybatis
</groupId>
-
<artifactId>mybatis-spring
</artifactId>
-
<version>1.3.1
</version>
-
</dependency>
-
<!--javaEE版本8.0-->
-
<dependency>
-
<groupId>javax
</groupId>
-
<artifactId>javaee-api
</artifactId>
-
<version>8.0
</version>
-
<scope>provided
</scope>
-
</dependency>
-
<!--mysql数据库的jdbc连接包-->
-
<dependency>
-
<groupId>mysql
</groupId>
-
<artifactId>mysql-connector-java
</artifactId>
-
<!--新版本使用最多的是6.0.6 但在我的配置中无法使用该版本,报一个很无解的异常-->
-
<version>5.1.39
</version>
-
</dependency>
-
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
-
<dependency>
-
<groupId>commons-dbcp
</groupId>
-
<artifactId>commons-dbcp
</artifactId>
-
<version>1.4
</version>
-
</dependency>
-
<!--jstl标签包-->
-
<dependency>
-
<groupId>jstl
</groupId>
-
<artifactId>jstl
</artifactId>
-
<version>1.2
</version>
-
</dependency>
-
<!--日志文件管理包——logStart-->
-
<dependency>
-
<groupId>log4j
</groupId>
-
<artifactId>log4j
</artifactId>
-
<version>${log4j.version}
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.slf4j
</groupId>
-
<artifactId>slf4j-log4j12
</artifactId>
-
<version>${slf4j.version}
</version>
-
<!--<scope>test</scope>-->
-
</dependency>
-
<dependency>
-
<groupId>org.slf4j
</groupId>
-
<artifactId>slf4j-api
</artifactId>
-
<version>${slf4j.version}
</version>
-
</dependency>
-
<!--日志文件管理包——logEnd-->
-
<!--格式化对象,方便输出日志-->
-
<dependency>
-
<groupId>com.alibaba
</groupId>
-
<artifactId>fastjson
</artifactId>
-
<version>1.2.42
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.codehaus.jackson
</groupId>
-
<artifactId>jackson-mapper-asl
</artifactId>
-
<version>1.9.13
</version>
-
</dependency>
-
<!--文件上传组件包-->
-
<dependency>
-
<groupId>commons-fileupload
</groupId>
-
<artifactId>commons-fileupload
</artifactId>
-
<version>1.3.3
</version>
-
</dependency>
-
<dependency>
-
<groupId>commons-io
</groupId>
-
<artifactId>commons-io
</artifactId>
-
<version>2.6
</version>
-
</dependency>
-
<dependency>
-
<groupId>commons-codec
</groupId>
-
<artifactId>commons-codec
</artifactId>
-
<version>1.11
</version>
-
</dependency>
-
</dependencies>
-
-
<build>
-
<finalName>SSMDemo
</finalName>
-
<pluginManagement>
<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
-
<plugins>
-
<plugin>
-
<artifactId>maven-clean-plugin
</artifactId>
-
<version>3.0.0
</version>
-
</plugin>
-
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
-
<plugin>
-
<artifactId>maven-resources-plugin
</artifactId>
-
<version>3.0.2
</version>
-
</plugin>
-
<plugin>
-
<artifactId>maven-compiler-plugin
</artifactId>
-
<version>3.7.0
</version>
-
</plugin>
-
<plugin>
-
<artifactId>maven-surefire-plugin
</artifactId>
-
<version>2.20.1
</version>
-
</plugin>
-
<plugin>
-
<artifactId>maven-war-plugin
</artifactId>
-
<version>3.2.0
</version>
-
</plugin>
-
<plugin>
-
<artifactId>maven-install-plugin
</artifactId>
-
<version>2.5.2
</version>
-
</plugin>
-
<plugin>
-
<artifactId>maven-deploy-plugin
</artifactId>
-
<version>2.8.2
</version>
-
</plugin>
-
</plugins>
-
</pluginManagement>
-
</build>
-
</project>
然后等待idea自动下载一堆jar的包,如果没有自动下载的可以点这里:
所有的包下载完成后控制台会有提示的。
*4.配置spring、spring和MyBatis的整合配置文件,最重要的一步也是最麻烦的一步同样也是最难的一步。不过不要怕,我们一步一步慢慢来。
这里内容有点多,就不多说啥了,直接粘代码,需要的复制过去就OK了。
spring-mybatis.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">
-
-
<!--加载配置文件-->
-
<context:property-placeholder location="classpath:config/jdbc.properties"/>
-
<!--配置数据源-->
-
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
-
<property name="driverClassName" value="${jdbc.driver}"/>
-
<property name="url" value="${jdbc.url}"/>
-
<property name="username" value="${jdbc.user}"/>
-
<property name="password" value="${jdbc.password}"/>
-
</bean>
-
<!--spring和mybatis整合-->
-
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-
<property name="dataSource" ref="dataSource"/>
-
<property name="typeAliasesPackage" value="com.**.model"/>
-
<property name="mapperLocations" value="classpath*:mapper/UserMapper.xml"/>
-
<!--<property name="configLocation" value="classpath:config/mybatis-config.xml"/>-->
-
</bean>
-
<!--扫描mapper接口包-->
-
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
-
<property name="basePackage" value="com.**.mapper"/>
-
<property name="sqlSessionFactoryBeanName" >
-
<idref bean="SqlSessionFactory"/>
-
</property>
-
</bean>
-
<!--service实现类扫描-->
-
<context:component-scan base-package="com.**.service"/>
-
<!--事务管理-->
-
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
-
<property name="dataSource" ref="dataSource"/>
-
</bean>
-
<tx:annotation-driven transaction-manager="transactionManager"/>
-
</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: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.xsd">
-
-
<!--启用spring的一些annotation -->
-
<context:annotation-config/>
-
<!--注册驱动-->
-
<mvc:annotation-driven>
-
<mvc:message-converters register-defaults="true">
-
<!-- 配置Fastjson支持 -->
-
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
-
<property name="supportedMediaTypes">
-
<list>
-
<value>application/json;charset=UTF-8
</value>
-
</list>
-
</property>
-
<property name="features">
-
<list>
-
<!--<value>WriteMapNullValue</value>-->
-
<value>WriteDateUseDateFormat
</value>
-
<value>QuoteFieldNames
</value>
-
</list>
-
</property>
-
</bean>
-
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
-
<property name="supportedMediaTypes">
-
<list>
<value>text/html;charset=UTF-8
</value>
</list>
-
</property>
-
</bean>
-
</mvc:message-converters>
-
</mvc:annotation-driven>
-
<!--<mvc:default-servlet-handler/>-->
-
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
-
<context:component-scan base-package="com.ssm.controller">
-
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
-
</context:component-scan>
-
-
<!--配置视图解析器-->
-
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
<property name="prefix" value="/WEB-INF/jsp/"/>
-
<property name="suffix" value=".jsp"/>
-
</bean>
-
<mvc:resources mapping="/lib/**" location="/lib"/>
-
-
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP-->
-
<!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
-
<!--<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
-
<!--<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>-->
-
<!--<property name="prefix" value="/WEB-INF/jsp/"/><!–设置JSP文件的目录位置–>-->
-
<!--<property name="suffix" value=".jsp"/>-->
-
<!--</bean>-->
-
-
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
-
<bean id="multipartResolver"
-
class=
"org.springframework.web.multipart.commons.CommonsMultipartResolver">
-
<!-- 默认编码 -->
-
<property name="defaultEncoding" value="utf-8"/>
-
<!-- 文件大小最大值 -->
-
<property name="maxUploadSize" value="10485760000"/>
-
<!-- 内存中的最大值 -->
-
<property name="maxInMemorySize" value="40960"/>
-
</bean>
-
-
</beans>
web.xml文件也是需要配置的,源码如下:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
-
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation=
"http://java.sun.com/xml/ns/javaee
-
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
-
version=
"3.0">
-
-
<display-name>Archetype Created Web Application
</display-name>
-
<!--spring和mybatis的配置文件-->
-
<context-param>
-
<param-name>contextConfigLocation
</param-name>
-
<param-value>classpath:/config/spring-mybatis.xml
</param-value>
-
</context-param>
-
-
<!-- 日志记录 -->
-
<context-param>
-
<!-- 日志配置文件路径 -->
-
<param-name>log4jConfigLocation
</param-name>
-
<param-value>classpath:config/log4j.properties
</param-value>
-
</context-param>
-
<context-param>
-
<param-name>log4jRefreshInterval
</param-name>
-
<param-value>60000
</param-value>
-
</context-param>
-
-
<!-- Spring字符集过滤器 -->
-
<filter>
-
<filter-name>SpringEncodingFilter
</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>
-
<init-param>
-
<param-name>forceEncoding
</param-name>
-
<param-value>true
</param-value>
-
</init-param>
-
</filter>
-
<filter-mapping>
-
<filter-name>SpringEncodingFilter
</filter-name>
-
<url-pattern>/*
</url-pattern>
-
</filter-mapping>
-
-
<!--spring监听器-->
-
<listener>
-
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
-
</listener>
-
<!--防止spring内存溢出监听器-->
-
<listener>
-
<listener-class>org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
-
</listener>
-
-
<!-- springMVC核心配置 -->
-
<servlet>
-
<servlet-name>spring
</servlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
-
<init-param>
-
<param-name>contextConfigLocation
</param-name>
-
<param-value>
-
classpath:config/spring-mvc.xml
-
</param-value>
-
</init-param>
-
<load-on-startup>1
</load-on-startup>
-
</servlet>
-
<servlet-mapping>
-
<servlet-name>spring
</servlet-name>
-
<url-pattern>/
</url-pattern>
-
</servlet-mapping>
-
-
<!--需要访问js文件时使用-->
-
<!--<servlet-mapping>-->
-
<!--<servlet-name>default</servlet-name>-->
-
<!--<url-pattern>*.js</url-pattern>-->
-
<!--</servlet-mapping>-->
-
-
</web-app>
log4j相信大家都不陌生,在项目中我们常常会用到日志,因此非常有必要配置一下日志信息。
log4j.properties源码如下:
log4j.rootLogger = DEBUG,Console,File log4j.appender.Console = org.apache.log4j.ConsoleAppender #log4j.appender.Console.Target = System.out log4j.appender.Console.layout = org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern = %5p %d %C: %m%n #每天打印一个日志文件 log4j.appender.File = org.apache.log4j.DailyRollingFileAppender log4j.appender.File.File=E:/logs/SSMDemo.log log4j.appender.File.Encoding=UTF-8 #打印所有级别的日志信息 log4j.appender.File.Threshold = ALL log4j.appender.File.layout = org.apache.log4j.PatternLayout log4j.appender.File.layout.ConversionPattern =%d{ABSOLUTE} %5p %c{1}\:%L - %m%n log4j.logger.com.opensymphony=ERROR log4j.logger.org.springframework=ERROR log4j.logger.org.apache=INFO log4j.logger.java.sql.Connection=INFO log4j.logger.java.sql.PreparedStatement=INFO log4j.logger.java.sql.ResultSet=INFO log4j.logger.java.sql.Statement=INFO
jdbc也是必不可少的一部分,毕竟数据都是来源于数据库的嘛。
jdbc.properties源码如下:
#开发环境 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mydatabase_test jdbc.user=your username jdbc.password=your password
到这里我们的配置文件就算说完了(当然这是最基础的需要,SSM的功能十分强大,还可以嵌入很多很多的功能...)
5.依葫芦画瓢,编写各层的功能类即可,controller层主要写http请求的接口方法,service主要写各种业务操作算法等,mapper层也可以称为dao层(我的习惯是写成mapper层)主要是写映射到数据库的方法接口,**Mapper.xml文件主要是写mapper层中的接口功能的SQL语句实现。这样的分层管理应该可以兼容各种大大小小的项目了。
下面也贴上我的工程中各层的测试代码。
model实体类:
-
package com.ssm.model;
-
-
/**
-
* created by viking on 2018/07/04
-
* user实体类
-
*/
-
-
public
class User {
-
private
int id;
-
private String name;
-
private
int age;
-
private String sex;
-
-
public User() {
-
}
-
-
public int getId() {
-
return id;
-
}
-
-
public void setId(int id) {
-
this.id = id;
-
}
-
-
public String getName() {
-
return name;
-
}
-
-
public void setName(String name) {
-
this.name = name;
-
}
-
-
public int getAge() {
-
return age;
-
}
-
-
public void setAge(int age) {
-
this.age = age;
-
}
-
-
public String getSex() {
-
return sex;
-
}
-
-
public void setSex(String sex) {
-
this.sex = sex;
-
}
-
-
@Override
-
public String toString() {
-
return
"User{" +
-
"id=" + id +
-
", name='" + name +
'\'' +
-
", age=" + age +
-
", sex='" + sex +
'\'' +
-
'}';
-
}
-
}
controller层:
-
package com.ssm.controller;
-
-
import com.ssm.model.User;
-
import com.ssm.service.UserService;
-
import org.apache.log4j.Logger;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.ResponseBody;
-
import org.springframework.web.bind.annotation.RestController;
-
-
import java.util.List;
-
-
/**
-
* created by viking on 2018/07/04
-
* controller层接口类
-
*/
-
@RestController
-
@RequestMapping(
"user")
-
public
class UserController {
-
@Autowired
-
private UserService userService;
-
Logger log = Logger.getLogger(UserController.class);
-
-
@RequestMapping(
"select")
-
@ResponseBody
-
public Object userTest(int id){
-
System.out.println(
"测试成功~~"+id);
-
List<User> user = userService.getUser(id);
-
System.out.println(user.toString());
-
log.info(user);
-
return user;
-
}
-
@RequestMapping(
"addUser")
-
public Object insertUser(int id,String name,int age,String sex){
-
userService.insertUser(id,name,age,sex);
-
return
"OK";
-
}
-
@RequestMapping(
"selectAll")
-
public Object selectAll(){
-
return userService.selectAll();
-
}
-
@RequestMapping(
"update")
-
public Object update(){
-
return userService.update();
-
}
-
-
}
service层(service层一般都是比较复杂的,因此我习惯分成接口和实现类两部分)接口:
-
package com.ssm.service;
-
-
import com.ssm.model.User;
-
-
import java.util.List;
-
/**
-
* created by viking on 2018/07/04
-
* 业务层接口
-
*/
-
public
interface UserService {
-
List<User> getUser(int id);
-
-
void insertUser(int id, String name, int age, String sex);
-
-
List<User> selectAll();
-
-
Object update();
-
}
service层实现类:
-
package com.ssm.service.impl;
-
-
import com.ssm.mapper.UserMapper;
-
import com.ssm.model.User;
-
import com.ssm.service.UserService;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Service;
-
-
import java.util.HashMap;
-
import java.util.List;
-
import java.util.Map;
-
/**
-
* created by viking on 2018/07/04
-
* 业务层接口实现类
-
*/
-
-
@Service
-
public
class UserServiceImpl implements UserService {
-
@Autowired
-
private UserMapper userMapper;
-
-
public List<User> getUser(int id) {
-
Map map =
new HashMap();
-
map.put(
"id",id);
-
return userMapper.getUser(map);
-
}
-
-
public void insertUser(int id, String name, int age, String sex) {
-
Map param =
new HashMap();
-
param.put(
"id",id);param.put(
"name",name);
-
param.put(
"age",age);param.put(
"sex",sex);
-
userMapper.insertUser(param);
-
}
-
-
public List<User> selectAll() {
-
return userMapper.selectAll();
-
}
-
-
public Object update() {
-
Map param =
new HashMap();
-
param.put(
"total",
10);
-
param.put(
"name",
"测试一");
-
userMapper.update(param);
-
return
"OK";
-
}
-
}
mapper层(dao层)接口:
-
package com.ssm.mapper;
-
-
import com.ssm.model.User;
-
import org.apache.ibatis.annotations.Param;
-
-
import java.util.List;
-
import java.util.Map;
-
/**
-
* created by viking on 2018/07/04
-
* dao层接口
-
*/
-
public
interface UserMapper {
-
List<User> getUser(@Param("param") Map map);
-
-
void insertUser(@Param("param") Map param);
-
-
List<User> selectAll();
-
-
void update(@Param("param") Map param);
-
}
**Mapper.xml MaBatsis映射文件配置文件,相当于mapper层接口的实现类了:
-
<?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.mapper.UserMapper">
-
<select id="getUser" parameterType="java.util.Map" resultType="com.ssm.model.User">
-
SELECT *
-
FROM user
-
where id =#{param.id}
-
</select>
-
<insert id="insertUser" parameterType="java.util.Map">
-
INSERT INTO user (id,name,age,sex) values(#{param.id},#{param.name},#{param.age},#{param.sex})
-
</insert>
-
<select id="selectAll" resultType="com.ssm.model.User">
-
SELECT * FROM USER
-
</select>
-
<update id="update" parameterType="java.util.Map">
-
UPDATE USER SET id=id-#{param.total},age=age+#{param.total}
-
WHERE name=#{param.name}
-
</update>
-
</mapper>
为了方便大家拷下代码后可以直接启动,我把数据库的sql语句和数据也贴上,我用的是mysql数据库:
-
/*
-
Navicat MySQL Data Transfer
-
-
Source Server : mysql
-
Source Server Version : 50173
-
Source Host : localhost:3306
-
Source Database : mydatabase_test
-
-
Target Server Type : MYSQL
-
Target Server Version : 50173
-
File Encoding : 65001
-
-
Date: 2018-06-15 15:41:23
-
*/
-
-
SET FOREIGN_KEY_CHECKS=
0;
-
-
-- ----------------------------
-
-- Table structure for user
-
-- ----------------------------
-
DROP
TABLE
IF
EXISTS
`user`;
-
CREATE
TABLE
`user` (
-
`id`
int(
11)
NOT
NULL,
-
`name`
varchar(
20)
DEFAULT
NULL,
-
`age`
int(
11)
DEFAULT
NULL,
-
`sex`
varchar(
10)
DEFAULT
NULL,
-
PRIMARY
KEY (
`id`)
-
)
ENGINE=
InnoDB
DEFAULT
CHARSET=utf8;
-
-
-- ----------------------------
-
-- Records of user
-
-- ----------------------------
-
INSERT
INTO
`user`
VALUES (
'2',
'测试二',
'22',
'男');
-
INSERT
INTO
`user`
VALUES (
'3',
'小二郎',
'18',
'男');
-
INSERT
INTO
`user`
VALUES (
'4',
'二郎',
'18',
'男');
-
INSERT
INTO
`user`
VALUES (
'5',
'马英',
'26',
'女');
-
INSERT
INTO
`user`
VALUES (
'6',
'王尼玛',
'30',
'男');
-
INSERT
INTO
`user`
VALUES (
'7',
'张三疯',
'109',
'男');
-
INSERT
INTO
`user`
VALUES (
'8',
'欧阳翻天',
'76',
'女');
-
INSERT
INTO
`user`
VALUES (
'9',
'上官',
'16',
'女');
-
INSERT
INTO
`user`
VALUES (
'10',
'李响',
'30',
'男');
-
INSERT
INTO
`user`
VALUES (
'11',
'测试一',
'0',
'女');
-
我本地的数据库表结构截图:
到这里就完全结束了,整个工程就可以直接运行了。
6.顺带提一下在idea中配置tomcat的过程(懂的朋友可以直接忽略~)
配置好了之后再将tomcat添加到server中:
好了~tomcat的配置也完成了。
7.下面贴上我的本机运行效果图:
tomcat成功启动:
通过浏览器访问localhost:
再测试下我们的SSM工程里测试类的正常工作截图:
控制台的输出和日志信息:
该有的输出都是正常的,再看一下我们保存到本地的日志文件:
成功地保存了我们工程的输出日志。
一晚上坚持一口气写完整个日志,到这里终于完美地结束了,如果大家觉得在下写的记录有帮助,可以给下支持和订阅哦!
附上GitHub的源码地址:SSMDemo GitHub源码
如果有不对的地方,欢迎各位大佬指教,如果有不懂的地方,也欢迎大家留言,我们一起讨论。
转载:https://blog.csdn.net/Mr__Viking/article/details/80919340