1.配置springMVC框架

第一步:导入包依赖

<!--配置springMVC-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>

依赖包

第二部:编写web.xml--设置前端控制器

<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>org.kong.config</param-value>
</init-param>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

web.xml

第三部:编写springMVC配置类

 1 package org.kong.config;
2 import org.springframework.context.annotation.ComponentScan;
3 import org.springframework.context.annotation.Configuration;
4 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
5
6 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
7
8 @SuppressWarnings("deprecation")
9 @Configuration
10 //开启包扫描注解
11 @ComponentScan(basePackages="org.kong")
12 //开启mvc注解驱动
13 @EnableWebMvc
14 public class MVCconfig extends WebMvcConfigurerAdapter {
15
16 }mvc

mvc配置类

第四部:编写controller组件类

 1 package org.kong.controller;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.web.bind.annotation.RequestMapping;
5
6 @Controller
7 public class Test {
8 @RequestMapping("/hellow")
9 public String hello() {
10 System.out.println("hellow");
11 return null;
12
13 }

通过url访问得到

2.配置Mybatis框架

项目导入依赖

 1 <dependency>
2 <groupId>org.apache.commons</groupId>
3 <artifactId>commons-dbcp2</artifactId>
4 <version>2.2.0</version>
5 </dependency>
6 <dependency>
7 <groupId>org.springframework</groupId>
8 <artifactId>spring-jdbc</artifactId>
9 <version>4.3.20.RELEASE</version>
10 </dependency>
11 <dependency>
12 <groupId>org.mybatis</groupId>
13 <artifactId>mybatis</artifactId>
14 <version>3.4.4</version>
15 </dependency>
16 <dependency>
17 <groupId>org.mybatis</groupId>
18 <artifactId>mybatis-spring</artifactId>
19 <version>1.3.1</version>
20 </dependency>
21 <dependency>
22 <groupId>mysql</groupId>
23 <artifactId>mysql-connector-java</artifactId>
24 <version>5.1.30</version>

配置mybatis配置类

package org.kong.config;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration
//3.动态创建代理对象
@MapperScan(basePackages="org.kong.mapper",annotationClass=Mapper.class)
@EnableTransactionManagement // <tx:annotaion-driver>
public class DataConfig {
@Value("${db.driverClassName}")
private String driverClassName; @Value("${db.url}")
private String url; @Value("${db.username}")
private String username; @Value("${db.password}")
private String password; // 1.数据源
@Bean
public DataSource getDataSource() {
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setDriverClassName(driverClassName);
basicDataSource.setUrl(url);
basicDataSource.setUsername(username);
basicDataSource.setPassword(password);
basicDataSource.setMaxTotal(10);
basicDataSource.setMaxWaitMillis(30000);
return basicDataSource;
}
// 2.会话工厂
@Bean
public SqlSessionFactory getSqlSessionFactory() {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
SqlSessionFactory sessionFactory = null;
try {
sqlSessionFactoryBean.setDataSource(this.getDataSource());
sqlSessionFactoryBean.afterPropertiesSet();
sessionFactory = sqlSessionFactoryBean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return sessionFactory;
}
//4.事务代理对象
@Bean
public DataSourceTransactionManager getTransactionManager() {
DataSourceTransactionManager tm=new DataSourceTransactionManager();
//指定数据源
tm.setDataSource(this.getDataSource());
return tm;
} }

测试

 1 package org.kong.test;
2
3 import java.sql.SQLException;
4 import java.util.List;
5 import java.util.Map;
6
7 import javax.sql.DataSource;
8
9 import org.junit.Test;
10 import org.junit.runner.RunWith;
11 import org.kong.config.ContextConfig;
12 import org.kong.config.DataConfig;
13 import org.kong.mapper.PapperMapper;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.test.context.ContextConfiguration;
16 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
17 import org.springframework.test.context.web.WebAppConfiguration;
18
19
20
21 @RunWith(SpringJUnit4ClassRunner.class)
22 @ContextConfiguration(classes={DataConfig.class,ContextConfig.class})
23 @WebAppConfiguration
24 public class Test01 {
25 @Autowired
26 private DataSource dataSource;
27 @Autowired
28 private PapperMapper papperMapper;
29
30
31 @Test
32 public void dataSource() {
33 try {
34 System.out.println(dataSource.getConnection());
35 } catch (SQLException e) {
36 // TODO Auto-generated catch block
37 e.printStackTrace();
38 }
39
40 }
41
42 @Test
43 public void hh() {
44 List<Map<String, Object>> fillALL = papperMapper.fillALL();
45
46 for (Map<String, Object> map : fillALL) {
47 System.out.println(map);
48 }
49 }
50
51
52 }

使用maven纯注解集成ssm的更多相关文章

  1. Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例

    Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例 转自:通过注解的方式集成Spring 4 MVC+Hibernate 4+MySQL+Maven,开发项目样例 ...

  2. maven 纯注解一步一步搭建Spring Mvc项目(入门)

    初次接触spring MVC项目,通过一段时间的学习,本文介绍一种以纯注解的方法去配置spring MVC环境,让那些配置的.xml文件统统见鬼吧. 什么是Spring MVC Spring MVC属 ...

  3. Spring4 MVC+Hibernate4+MySQL+Maven使用注解集成实例

    在本教程中,我们将使用基于注解的配置集成Spring和Hibernate. 我们将开发包含表单要求用户输入一个简单的CRUD为导向Web应用程序,使用Hibernate保存输入的数据到 MySQL 数 ...

  4. Spring MVC4 纯注解配置教程

    阅读本文需要又一定的sping基础,最起码要成功的运行过一个SpringMvc项目. 在传统的Spring项目中,我们要写一堆的XML文件.而这些XML文件格式要求又很严格,很不便于开发.而网上所谓的 ...

  5. springboot2.x纯注解整合dubbo

    springboot1.x和springboot2.x整合差距挺大的,基于最新的2.x进行整合,使用纯注解的方式 依赖选取 首先pom文件的依赖引入,maven仓库有Apache和alibaba两个 ...

  6. Shiro集成SSM基于动态URL权限管理(二)

    这个案例基于上一个demo扩展而来.所以数据库表,在Shiro集成SSM基于URL权限管理(一)开篇的一致.如果上个demo操作的建议重新导入一次,避免出现问题. 而这次都不是通过固定写在方法上的注解 ...

  7. spring boot纯注解开发模板

    简介 spring boot纯注解开发模板 创建项目 pom.xml导入所需依赖 点击查看源码 <dependencies> <dependency> <groupId& ...

  8. 学习笔记——Maven实战(四)基于Maven的持续集成实践

    Martin的<持续集成> 相信很多读者和我一样,最早接触到持续集成的概念是来自Martin的著名文章<持续集成>,该文最早发布于2000年9月,之后在2006年进行了一次修订 ...

  9. maven,spring,mybatis集成错误

    maven,spring,mybatis集成的时候单元测试junit测试没问题,但mvn jetty:run 就报错误 错误: org.apache.ibatis.binding.BindingExc ...

随机推荐

  1. 浅析 MVC

    MVC(Model–View–Controller) Model:数据模型  负责操作所有数据View:视图 负责所有UI界面Controller:控制器 负责其他 //数据放在m const m = ...

  2. html行内块元素之间的缝隙

    关于html行内块元素之间缝隙的那点儿事情 事情是这样子的,我起初打算验证使用transform属性的标签是否会影响其他的标签的布局,于是写了下面一段代码: <!DOCTYPE html> ...

  3. formily-面向中后台场景的复杂解决方案

    正文 在解决企业级应用的前端问题中,表单是个无法绕过的大山,正好最近有时间,调研一下 Formily-来自阿里巴巴的面向中后台复杂场景的表单解决方案,也是一个表单框架,前身是 UForm.主要解决如何 ...

  4. Linux下如何知道是否有人在使坏?

    在 Linux 下查看用户的行为,不仅仅是网管要做的事,也是开发人员所应该具备的基本技能之一.为什么呢?因为有时其他同事在做一些很消耗资源的事情,比如在编译大型程序,可能会导致服务器变得很慢,从而影响 ...

  5. 日志记录——logging模块

    Logging:日志记录是为了跟踪记录软件运行时,发生的事件,包括出错,提示信息等等.log日志级别:日志级别大小关系为:CRITICAL > ERROR > WARNING > I ...

  6. Docker 镜像的备份恢复迁移

    在大家已经学会了如何构建镜像以后,为了备份该镜像,我们有以下几个选择: 我们可以将指定镜像保存成 tar 归档文件,需要使用时将 tar 包恢复为镜像即可: 登录 DockerHub 注册中心,将镜像 ...

  7. Mybatis 枚举类处理

    目录 类型处理器(TypeHandler) 内置的枚举处理器 EnumTypeHandler源码 自定义枚举类处理 通用枚举处理器 Git 类型处理器(TypeHandler) 无论是 MyBatis ...

  8. Codeforces 1389 题解(A-E)

    AC代码 A. LCM Problem 若\(a < b\),则\(LCM(a,b)\)是\(a\)的整数倍且\(LCM(a,b) \ne a\),所以\(LCM(a,b) \ge 2a\),当 ...

  9. 3. 站在使用层面,Bean Validation这些标准接口你需要烂熟于胸

    乔丹是我听过的篮球之神,科比是我亲眼见过的篮球之神.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免 ...

  10. Tomcat三实例cluster多播方案共享session再配置

    昨天已经将两实例cluster多播方案共享Session配置成功,其中的关键就在于server.xml中,engine->channel->receiver节点中address得写成自己的 ...