使用maven纯注解集成ssm
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>
第三部:编写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
第四部:编写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 }