本文共 5397 字,大约阅读时间需要 17 分钟。
一、整体步骤
maven建好web工程并配置好整体包架构(三层架构)->pom文件中所需依赖导入->配置web.xml->spring-mvc配置->配置spring-mybatis配置->建立相应代码和数据表进行测试
整体结构如下
1.maven新建项目,在新建项目时加入一组东西,下载依赖会快
(
archetypeCatalog
internal
)
2.然后补充好java源包,resources配置包,三层架构包
3.配置pom.xml文件
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.yiyezhiqiu
ssm01
1.0-SNAPSHOT
war
ssm01 Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
junit
junit
4.11
test
javax.servlet
javax.servlet-api
4.0.0
provided
org.springframework
spring-webmvc
5.0.8.RELEASE
org.springframework
spring-jdbc
5.0.8.RELEASE
mysql
mysql-connector-java
5.1.38
com.mchange
c3p0
0.9.5.2
org.mybatis
mybatis
3.4.6
org.mybatis
mybatis-spring
1.3.1
jstl
jstl
1.2
ch.qos.logback
logback-classic
1.2.3
ch.qos.logback
logback-core
1.2.3
org.logback-extensions
logback-ext-spring
0.1.4
ssm01
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
3.配置web.xml文件(比如前端控制器DispatcherServlet)
/p>
"http://java.sun.com/dtd/web-app_2_3.dtd" >
version="2.5">
Archetype Created Web Application
contextConfigLocation
classpath:spring-mybatis.xml
logbackConfigLocation
classpath:logback.xml
ch.qos.logback.ext.spring.web.LogbackConfigListener
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
springmvc
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/*
4.配置spring-mvc.xml (整合spring和springmvc)
application/json;charset=utf-8
text/html;charset=utf-8
application/x-www-form-urlencoded
5.配置spring-mybatis(spring和mybatis整合)
5.1 、写一个jdbc.properties文件存放和mysql数据库连接的字符串信息
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF8
mysql.username=root
mysql.password=root
initPoolSize=3maxPoolSizes=30
5.2、配置spirng和mybatis
xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
6.创建代码和表
6.1、Users
packagecom.yiyezhiqiu.domain;importjava.io.Serializable;public class Users implementsSerializable {privateInteger id;privateInteger uid;privateString userName;privateString password;publicUsers(Integer id, Integer uid, String userName, String password) {this.id =id;this.uid =uid;this.userName =userName;this.password =password;
}publicInteger getId() {returnid;
}public voidsetId(Integer id) {this.id =id;
}publicInteger getUid() {returnuid;
}public voidsetUid(Integer uid) {this.uid =uid;
}publicString getUserName() {returnuserName;
}public voidsetUserName(String userName) {this.userName =userName;
}publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}
@OverridepublicString toString() {return "Users{" +
"id=" + id +
", uid=" + uid +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
'}';
}
}
6.2、UsersController
@RestController
@RequestMapping(value= "/users")public classUsersController {
@Autowired
IUsersService usersService;
@RequestMapping(value= "/saveUsers",method =RequestMethod.GET)
@ResponseBodypublicString saveUsers(Users users){
System.out.println("users:"+users);int result =usersService.saveUsers(users);if(result>0){return"保存成功";
}else{return"保存失败";
}
}
}
6.3、UsersDao
@Repositorypublic interfaceUsersDao {intsaveUsers(Users users);
}
6.4、UsersDao.xml
insert into users(uid,userName,password) values (#{uid},#{userName},#{password})
6.5、mysql表创建相应表和字段即可。
6.6、测试访问即可。
补充:加入lockback日志:
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [line%4line] %logger{50} : %msg%n
UTF-8
${path}/info/info-%d{yyyy-MM-dd}.%i.log
64 MB
INFO
false
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [line%4line] %logger{50} : %msg%n
UTF-8
${path}/warn/warn-%d{yyyy-MM-dd}.%i.log
64 MB
WARN
false
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [line%4line] %logger{50} : %msg%n
UTF-8
${path}/error/error-%d{yyyy-MM-dd}.%i.log
64 MB
ERROR
false
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [line%4line] %logger{50} : %msg%n
UTF-8
${path}/debug/debug-%d{yyyy-MM-dd}.%i.log
64 MB
DEBUG
false
${CONSOLE_LOG_PATTERN}
UTF-8
DEBUG
总结:
1.大体创建步骤...
2.细节
2.1、Controller层,展示视图:@Controller , 展示字符串形式:@RestController
2.2、聊聊我们新添加一个配置文件,为啥就要上面一大串的约束:它的作用主要是spring加载配置文件时验证配置文件的语法正确性。
这片博客讲解:
转载地址:http://hdima.baihongyu.com/