什么是SSM框架?
ssm是基于Spring、Spring MVC以及mybatis3个开源框架整合的系统架构,遵循标准的MVC设计模式,是web项目常用的技术架构。
Spring
个人理解,spring在ssm整套架构主要承担的是管理的角色。其核心是IoC(控制反转)容器,IoC容器使用工厂模式(BeanFactory)维护着对象的创建、依赖关系以及生命周期等。简单点说,IoC容器帮助开发者省去了new操作,直接可以通过依赖注入(DI)的方式调用对象,而不用管对象是怎么产生的。
此外,Spring提供了一种“横切”技术——面向切面编程(AOP),其底层采用的是动态代理模式(Dynamic Proxy),通过预编译方式和运行期动态代理实现功能的统一维护。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
Spring MVC
Spring MVC其实是Spring大家庭中的一员,采用的经典的MVC设计模式。这里单独拎出来说,只要是Spring MVC在ssm框架中负责的是整个业务流程的生命周期。如何理解这一点呢?下面套用下Spring MVC的工作流程:
1)用户向服务器发送请求,请求被Spring前端控制Servelt DispatcherServlet捕获;
2)DispatcherServlet对请求URL进行解析,得到请求资源标识符(URI)。然后根据该URI,调用HandlerMapping获得该Handler配置的所有相关的对象(包括Handler对象以及Handler对象对应的拦截器),最后以HandlerExecutionChain对象的形式返回;
3)DispatcherServlet 根据获得的Handler,选择一个合适的HandlerAdapter。(附注:如果成功获得HandlerAdapter后,此时将开始执行拦截器的preHandler(…)方法)
4)提取Request中的模型数据,填充Handler入参,开始执行Handler(Controller)。 在填充Handler的入参过程中,根据你的配置,Spring将帮你做一些额外的工作:
HttpMessageConveter: 将请求消息(如Json、xml等数据)转换成一个对象,将对象转换为指定的响应信息;
数据转换:对请求消息进行数据转换。如String转换成Integer、Double等;
数据根式化:对请求消息进行数据格式化。 如将字符串转换成格式化数字或格式化日期等;
数据验证: 验证数据的有效性(长度、格式等),验证结果存储到BindingResult或Error中。
5)Handler执行完成后,向DispatcherServlet 返回一个ModelAndView对象;
6)根据返回的ModelAndView,选择一个适合的ViewResolver(必须是已经注册到Spring容器中的ViewResolver)返回给DispatcherServlet ;
7)ViewResolver 结合Model和View,来渲染视图;
8)将渲染结果返回给客户端。
MyBatis
Mybatis是个半持久层框架,主要负责与数据库进行数据交互。Mybaits更加专注于SQL本身,着力于POJO 与SQL之间的映射关系。然后通过映射配置文件,将SQL所需的参数(输入映射),以及返回的结果字段映射到指定POJO(输出映射),是一种“SQL Mapping“的ORM实现。
相较Hibernate来说,虽然Mybatis映射的能力弱点,但是入门门槛也较低,主要是Mybatis够灵活,个人推荐。
瞎bb了这么多,下面就上手操作吧。上一文《初识maven》简单的介绍了一下maven,现在我们将基于maven将ssm框架搭建起来。开动~
先将项目测试的编译输出目录修改下,怕编译结果混在一起了,操作如下:
项目右键》Build Path》configure Build Path,修改src/test/java和src/test/resource两个编译输出目录为target/test-classess。
修改项目的版本配置,将web配置版本调整为2.5,java为1.8。但是会出现下面这种更改不了的情况
这时需要将Dynamic Web Module去除后保存,然后重新勾选版本
并修改路径为src/main/webapp。同时,将web.xml内容修改如下:
<?xml version=\”1.0\” encoding=\”UTF-8\”?>
<!DOCTYPE xml>
<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_2_5.xsd\”
id=\”WebApp_ID\” version=\”2.5\”>
</web-app>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
接着完善下项目的目录结构(仅代表个人的习惯)
然后在pom.xml中配置相关的引用。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<!– mybatis核心包 –>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!—jackjson,支持接口返回输出json格式 –>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.1</version>
</dependency>
<!–打印日志 –>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
在src/main/resources目录下添加项目所需的配置信息和配置文件,然后修改web.xml配置文件。
1.数据库信息(database.properties)
dataSource.username= ${数据库账号}
dataSource.password= ${数据库密码}
dataSource.databaseName= ${数据库名称}
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.dialect=org.hibernate.dialect.MySQL5Dialect
dataSource.serverName=
dataSource.url=jdbc:mysql://${dataSource.serverName}/${dataSource.databaseName}?useUnicode=true&characterEncoding=UTF-8&useOldAliasMetadataBehavior=true&useSSL=false
dataSource.properties=user=${dataSource.username};databaseName=${dataSource.databaseName};serverName=${dataSource.serverName};password=${dataSource.password}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2.日志配置(log4j.properties)
log4j.rootLogger=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=[%c]%m%n
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=../log/error.log
log4j.appender.File.MaxFileSize=10MB
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH:mm:ss,SSS}][%c]%m%n
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
3.spring-mvc.xml
<?xml version=\”1.0\” encoding=\”UTF-8\”?>
<beans xmlns=\”http://www.springframework.org/schema/beans\”
xmlns:p=\”http://www.springframework.org/schema/p\”
xmlns:aop=\”http://www.springframework.org/schema/aop\”
xmlns:context=\”http://www.springframework.org/schema/context\”
xmlns:mvc=\”http://www.springframework.org/schema/mvc\”
xmlns:tx=\”http://www.springframework.org/schema/tx\”
xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\”
xsi:schemaLocation=\”http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd\”>
<!– 把标记了@Controller注解的类转换为bean –>
<context:component-scan base-package=\”com.maven4web.controller\” />
<context:annotation-config />
<!– 解决@ResponseBody中文乱码 –>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class=\”org.springframework.http.converter.StringHttpMessageConverter\”>
<constructor-arg value=\”UTF-8\” />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!– 启动切面注入代理 –>
<aop:aspectj-autoproxy proxy-target-class=\”true\” />
<!– 启动Spring MVC的注解功能,完成请求和注解POJO的映射 –>
<bean class=\”org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter\”/>
<!– 对模型视图名称的解析,即在模型视图名称添加前后缀 –>
<bean class=\”org.springframework.web.servlet.view.InternalResourceViewResolver\”
p:prefix=\”/view/\” p:suffix=\”.jsp\” />
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
4.spring-content.xml
<?xml version=\”1.0\” encoding=\”UTF-8\”?>
<beans xmlns=\”http://www.springframework.org/schema/beans\”
xmlns:aop=\”http://www.springframework.org/schema/aop\”
xmlns:context=\”http://www.springframework.org/schema/context\”
xmlns:p=\”http://www.springframework.org/schema/p\”
xmlns:tx=\”http://www.springframework.org/schema/tx\”
xmlns:mvc=\”http://www.springframework.org/schema/mvc\”
xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\”
xmlns:task=\”http://www.springframework.org/schema/task\”
xsi:schemaLocation=\”
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd \”>
<!– 扫描包 –>
<context:component-scan base-package=\”com.maven4web\” />
<context:annotation-config />
<mvc:annotation-driven />
<!– 数据源 –>
<context:property-placeholder location=\”classpath*:/database.properties\”
ignore-unresolvable=\”true\” />
<bean id=\”dataSource\”
class=\”org.springframework.jdbc.datasource.DriverManagerDataSource\”>
<property name=\”driverClassName\” value=\”${dataSource.driverClassName}\” />
<property name=\”url\” value=\”${dataSource.url}\” />
<property name=\”username\” value=\”${dataSource.username}\” />
<property name=\”password\” value=\”${dataSource.password}\” />
</bean>
<!– 会话工厂 –>
<bean id=\”sqlSessionFactory\” class=\”org.mybatis.spring.SqlSessionFactoryBean\”>
<property name=\”dataSource\” ref=\”dataSource\”></property>
<property name=\”configLocation\” value=\”classpath:mybatis-config.xml\”></property>
<property name=\”mapperLocations\” value=\”classpath:com/maven4web/mapper/impl/*.xml\”></property>
</bean>
<!– 事务管理器 –>
<bean id=\”txManager\”
class=\”org.springframework.jdbc.datasource.DataSourceTransactionManager\”>
<property name=\”dataSource\” ref=\”dataSource\” />
</bean>
<!– 事务注解驱动,注解@Transactional的类和方法将具有事务性 –>
<tx:annotation-driven transaction-manager=\”txManager\” />
<!– spring和mybatis的整合 –>
<bean class=\”org.mybatis.spring.mapper.MapperScannerConfigurer\”>
<property name=\”basePackage\” value=\”com.maven4web.mapper\”></property>
</bean>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
5.web.xml
<?xml version=\”1.0\” encoding=\”UTF-8\”?>
<!DOCTYPE xml>
<web-app xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\”
xmlns=\”http://java.sun.com/xml/ns/j2ee\” xmlns:web=\”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_2_5.xsd\”
id=\”WebApp_ID\” version=\”2.5\”>
<display-name>maven4web</display-name>
<!– 工程编码过滤器 –>
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!– log4j配置 –>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<!– Spring容器加载所有的配置文件的路径 –>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-context.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!– 配置SpringMVC核心控制器 –>
<servlet>
<servlet-name>springMVC</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>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<!–为DispatcherServlet建立映射 –>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.eot</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.svg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ttf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.woff</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.woff2</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.txt</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/index.jshtml</url-pattern>
</servlet-mapping>
<!– 防止Spring内存溢出监听器 –>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>
<!– 起始页 –>
<welcome-file-list>
<welcome-file>index.jshtml</welcome-file>
</welcome-file-list>
</web-app>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
配置完这些,下面实现个简单的功能来验证这个框架是否可用。
首先,在数据库执行下面的SQL创建一张名为t_test的表。
CREATE TABLE `t_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT \’\’,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
- 1
- 2
- 3
- 4
- 5
- 6
然后,在项目model下创建一个名为Test的实体。
src/main/java/model/TestMapper.java
public class Test {
private Integer id;
private String username;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
完后。在mapper添加对Test这个是数据库相关操作的接口声明,同时在同级目录下新建一个impl的package,放入该接口的实现。
src/main/java/mapper/TestMapper.java
public interface TestMapper {
Integer insert(@Param(\”username\”) String username);
List<Test> getAll();
}
- 1
- 2
- 3
- 4
src/main/java/mapper/impl/TestMapper.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.maven4web.mapper.TestMapper\”>
<select id=\”getAll\” resultType=\”com.maven4web.model.Test\”>
select
t.id,
t.username
from
t_test t
</select>
<insert id=\”insert\” parameterType=\”String\”>
insert into t_test(
username
)
values(
#{username}
)
</insert>
</mapper>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
然后在controller下新建一个TestController。
src/main/java/controller/TestController.java
@Controller
public class TestController {
@Autowired
private TestService testService;
@RequestMapping(\”/test.action\”)
@ResponseBody
public List<?> test(
@RequestParam(value=\”username\”, required=true) String username
){
testService.insert(username);
List<Test> tests = testService.getAll();
return tests;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
再在service新建业务功能TestService接口,同时在同级目录下新建个impl,放入其实现类。
src/main/java/service/TestService.java
public interface TestService {
void insert(String username);
List<Test> getAll();
}
- 1
- 2
- 3
- 4
src/main/java/service/impl/TestServiceImpl.java
@Service
@Transactional
public class TestServiceImpl implements TestService {
@Autowired
private TestMapper testMapper;
@Override
public void insert(String username) {
testMapper.insert(username);
}
@Override
public List<Test> getAll() {
return testMapper.getAll();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
到此整合和实例基本完成,可以把项目跑起来看看效果了。验证方式在浏览器输入http://localhost:8080/maven4web/test.action?username=hello,看浏览器是否返回:
[{\”id\”:1,\”username\”:\”hello\”}]
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。