spring和springMVC整合
1:spring.xml配置.
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描的包,不需要处理Controller注解-->
<context:component-scan base-package="hcs.itcast">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<import resource="mybatis.xml"></import>
</beans>
SpringMvc 配置
1:WEB.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--解决Post请求中的乱码问题.. -->
<filter>
<filter-name>characterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern> / *</url-pattern>
</filter-mapping>
<!--监听器ContextLoaderListener -->
<!--默认加载WEB-INF目录下 [方法1]将你的spring配置文件放到WEB-INF.[方法2]通过context-param属性加载类路径[推荐] -->
<!--spring和springMVC的整合,通过监听器在加载springmvc.xml时也加载spring.xml的配置文件 -->
<!--ServletContext域对象的创建于销毁【spring-web.jar】 -->
<!-- 将Servlet和Controller放到容器中-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!--前端控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载spingmvc配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--启动服务器,创建该servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2:SpringMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描,只处理Controller -->
<context:component-scan base-package="hcs.itcast">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--视图解析器 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--过静态资源 -->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--开启注解的支持 -->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
Spring和mybatis
配置文件+映射
<?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"
xsi:schemaLocation="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.xsd">
<!--配置扫描 -->
<context:component-scan base-package="hcs.itcast"></context:component-scan>
<!--连接数据库 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/student?useSSL=false&characterEncoding=UTF-8&useUnicode=true&serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--加载配置文件【sqlsessionFactorbean】 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:hcs.itcast.dao/*.xml"></property>
</bean>
<!--配置接口所在包 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
<property name="basePackage" value="hcs.itcast.dao"></property>
</bean>
</beans>
也可以通过注解@Select,@Insert…配置
<?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="hcs.itcast.dao.AccountDao">
<select id="FindAllInfo" resultType="hcs.itcast.model.Account">
select *from account
</select>
<insert id="SaveAccount" parameterType="hcs.itcast.model.Account">
insert into account(stuname,money) values (#{stuname},#{money})
</insert>
</mapper>
Controlller控制器
@Controller
@RequestMapping("/account")
public class AccountControll {
@Autowired
private AccountService acc;
@RequestMapping("/test01")
public String account(Model model){
System.out.println("执行了查询用户");
System.out.println(acc);
List<Account> accounts = acc.SFindAllInfo();
System.out.println("结果"+accounts);
model.addAttribute("liststy",accounts);
return "succes";
}
}
<!--配置Spring框架声明式事务 -->
<!--配置事务管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务通知 -->
<tx:advice id="txtadvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<tx:method name="Find*" read-only="true"/><!--方法以Find 以只读方式 -->
<tx:method name="*" isolation="DEFAULT"></tx:method>
</tx:attributes>
</tx:advice>
<!--配置Aop配置 -->
<aop:config>
<aop:advisor advice-ref="txtadvice" pointcut="execution(* cn.itcast.servlet.impl.*ServicImpl.*(..))"></aop:advisor>
</aop:config>