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. 第三篇Scrum冲刺博客--Interesting-Corps

    第三篇Scrum冲刺博客 站立式会议 1.会议照片 2.队友完成情况 团队成员 昨日完成 今日计划 鲍鱼铭 主页页面跳转社区功能及社区设计及布局实现 搜索页面跳转.设计及布局实现 叶学涛 编写个人页面 ...

  2. wc.exe程序

    1.gitHub地址:https://github.com/loveYuJun/wc.exe.git 2.PSP表格 psp2.1 Personal Software Process Stages 预 ...

  3. Privacy-Preserving Deep Learning via Additively Homomorphic Encryption

    郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! Full version of a paper at the 8-th International Conference on Appli ...

  4. Spring Cloud--尚硅谷2020最新版

    Spring Cloud 初识Spring Cloud与微服务 在传统的软件架构中,我们通常采用的是单体应用来构建一个系统,一个单体应用糅合了各种业务模块.起初在业务规模不是很大的情况下,对于单体应用 ...

  5. 《MySQL数据库》MySQL主从复制搭建与原理

    前言 主从复制:两台或者更多的数据库实例,通过二进制日志,实现数据同步.为什么需要主从复制,主从复制的作用是什么,答:为了预防灾难. 搭建 第一步:准备多实例环境.如何创建多实例见: 第二步:确保每一 ...

  6. 力扣Leetcode 面试题56 - I. 数组中数字出现的次数

    面试题56 - I. 数组中数字出现的次数 一个整型数组 nums 里除两个数字之外,其他数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度是O(n),空间复杂度是O(1). 示例 ...

  7. AD18使用原理图优先选项( Preference)调整原理图纸张大小失效问题解决

    1.创建新的原理图纸后,在当前点击更改并不会生效 2.想要生效需要去原理图纸的文档详细属性中更新即可生效!以下两种方式可以打开文档选项按钮. a.O->D 打开文档选项 b.右下角选择Prope ...

  8. 4-6年经验左右、优秀的 Java 程序员应该具备的技能

    4-6年经验左右.优秀的 Java 程序员应该具备的技能有哪些,按“专业技能”和“项目”两块,包括但不限于以下内容. 专业节能方面 基础:JDK 常用类的原理.源码.使用场景. 设计模式:常用几种的原 ...

  9. IntPtr to bytes

    byte[] managedArray = new byte[size]; Marshal.Copy(pnt, managedArray, 0, size);

  10. asp image

    @Url.Content("~/Content/images/myimage.png") <img src="~/Content/images/myimage.pn ...