ssm整合测试,当了解

1.建立工程文件 然后导入包

2.配置web.xml文档

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <display-name>MyBatis_06_ssm</display-name>
  
  <!--Spring配置: needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- SpringMVC配置 -->
	<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
  
</web-app>

3. 配置 spring-servlet.xml文件(springmvc配置文件)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<!--SpringMVC只是控制网站跳转逻辑  -->
	<!-- 只扫描控制器 -->
	<context:component-scan base-package="com.smgk.mybatis" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<mvc:annotation-driven></mvc:annotation-driven>
	<mvc:default-servlet-handler/>
</beans>

4.配置mybatis的文档  (mybatis-config.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<settings>
		<setting name="mapUnderscoreToCamelCase" value="true"/>
		<setting name="jdbcTypeForNull" value="NULL"/>
		
		<!--显式的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题  -->
		<setting name="cacheEnabled" value="true"/>
		<setting name="lazyLoadingEnabled" value="true"/>
		<setting name="aggressiveLazyLoading" value="false"/>
	</settings>
	
</configuration>

5.配置 spring 文件,最复杂,

5.1)配置spring要管理的包,即扫描包,不扫描包含 controller注解的包

	<!-- Spring希望管理所有的业务逻辑组件,等。。。 -->
	<context:component-scan base-package="com.smgk.mybatis">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

5.2)引入数据库配置文件

<context:property-placeholder location="classpath:dbconfig.properties" />

5.2.3)引入配置文件后就配置c3p0连接池

<!-- Spring用来控制业务逻辑。数据源、事务控制、aop -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>

5.3)配置spring事务管理器

<!-- spring事务管理 -->
	<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 控制事务的开启和关闭,要达到控制事务必先控制 数据库  下面这行代码就是直接引用beanId=dataSource的控制数据源 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>

5.4)开启基于注解的事务(transaction-manager对应上面的bean 的id)

<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>

5.5)创建sqlSessionFactory 对象,用于mybatis操作数据库

<!-- 
	    整合mybatis 
		目的:1、spring管理所有组件。mapper的实现类。
				service==>Dao   @Autowired:自动注入mapper;
			2、spring用来管理事务,spring声明式事务
	-->
	<!--创建出SqlSessionFactory对象  -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<!-- configLocation指定全局配置文件的位置 -->
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
		<!--mapperLocations: 指定mapper文件的位置-->
		<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
	</bean>

<!--配置一个可以进行批量执行的sqlSession  -->
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>
		<constructor-arg name="executorType" value="BATCH"></constructor-arg>
	</bean>

5.6)配置一个可以进行批量的sqlSession

代码在上面,

5.7)扫描所有的mapper接口的实现,让这些mapper能够自动注入(即扫描dao包的接口)

    <mybatis-spring:scan base-package="com.smgk.mybatis.dao"/>

6. 建立 dao包存放(mapper接口) 这里的到就是要绑定到mybatisxml中和数据库绑定的接口

public interface StudentMapper {
	//提供两个操作数据库的方法
	public Student getStudentById(Integer id);
	
	public List<Student>getStus();
	
}

6.1)建立  StudentMapper.xml  与接口中的StudentMapper接口对应(conf/mybatis.mapper包下)

<?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.smgk.mybatis.dao.StudentMapper"><!--这值 要与对应的接口全名对应-->
	<!--   public Student getStudentById(Integer id);   -->
	<select id="getStudentById" resultType="com.smgk.mybatis.bean.Student">
		select * from tbl_student_crud where id=#{id}
	</select>
	<!--public List<Employee> getEmps();  -->
	<select id="getStus" resultType="com.smgk.mybatis.bean.Student">
		select * from tbl_student_crud
	</select>
</mapper>

7  建立 service 包(服务层)StudentService

@Service
public class StudentService {
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private SqlSession sqlSession;

	public List<Student> getStus(){
		System.out.println("[StudentService] k上的List运行 ");
//因为获取sqlSession这些已经交给spring了,已经在spring.xml文件中处理了,只要一调用这条方法就会返回所有学生信息
		return studentMapper.getStus();
	}
}

8.建立 Controller层  StudentController   这一层的功能是直接对应web的,这层与springmvc对立

@Controller
public class StudentController {
	@Autowired
	StudentService studentService;
	
	@RequestMapping("/stus")
	public String getStus(Map<String,Object>map){
		System.out.println("[StudentController] k上的stus运行");
		List<Student> stus=studentService.getStus();
		map.put("allStus", stus);
		System.out.println("[StudentController] k上的stus运行结束 ");
		return "list";
	}
}

9.建立 bean bean的字段要与数据库对立  其实第一就要建立 他的

public class Student {
	
	private Integer id;
	private String lastName;
	private String email;
	private String gender;
	private String city;
	private Department dept;
//   get set  toString constructor 
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值