这个博客还是以传智播客的杰信项目为材料,特别在此说明。
第一步:先导入jar包:用maven的方式来导入jar包。在001杰信-创建MyEclipse与maven项目博客 中提到过。
第二步:项目结构如下:
config包下面的结构如下:
第三步:先整合spring和mybatis.
先编写beans.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:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 1.加载数据库配置的属性文件 --> <context:property-placeholder location="classpath:/jdbc/jdbc.properties"/> <!-- 2. 包扫描dao,service 把bean注册到bean容器中
--> <context:component-scan base-package="cn.itcast.jk.dao,cn.itcast.jk.service"/> <!-- 3. 数据源dataSource C3P0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxPoolSize" value="${c3p0.pool.maxPoolSize}"/> <property name="minPoolSize" value="${c3p0.pool.minPoolSize}"/> <property name="initialPoolSize" value="${c3p0.pool.initialPoolSize}"/> <property name="acquireIncrement" value="${c3p0.pool.acquireIncrement}"/> </bean> <!-- 4. SessionFactory
用mybatis框架提供的类的给mybatis配置好。这样的话在Dao层就可以直接 BaseDaoImpl<T> extends SqlSessionDaoSupport 通过继承SqlSessionDaoSupport。
从而通过this.getSqlSession()函数来操作数据库。
--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 整合mybatis,包扫描 mapper文件 --> <property name="configLocation" value="classpath:/mybatis/sqlMapConfig.xml"/> </bean> <!-- 5. 事务 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="find*" read-only="true"/> <tx:method name="get*" read-only="true"/> <tx:method name="view*" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* cn.itcast.jk.service.*.*(..))" id="txPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> </beans>
再编写classpath:/mybatis/sqlMapConfig.xml下面的sqlMapConfig.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> <!-- 把映射文件(sqlmap/user.xml)加载进sqlMapConfig.xml--> <mappers> <mapper resource="mybatis/FactoryMapper.xml"/> </mappers> </configuration>
再编写mybatis/FactoryMapper.xml下面的FactoryMapper.xml文件:
<?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="cn.itcast.jk.mapper"> <!-- factory_c表的映射 --> <resultMap type="cn.itcast.jk.domain.Factory" id="factoryRM" > <id property="id" column="FACTORY_ID"/> <result property="fullName" column="FULL_NAME"/> <result property="factoryName" column="FACTORY_NAME"/> <result property="contacts" column="CONTACTS"/> <result property="phone" column="PHONE"/> <result property="mobile" column="MOBILE"/> <result property="fax" column="FAX"/> <result property="cnote" column="CNOTE"/> <result property="inspector" column="INSPECTOR"/> <result property="orderNo" column="ORDER_NO"/> <result property="createBy" column="CREATE_BY"/> <result property="createDept" column="CREATE_DEPT"/> <result property="createTime" column="CREATE_TIME"/> </resultMap> <select id="find" parameterType="map" resultMap="factoryRM" > select * from factory_c where 1=1 </select> <!-- 当数据库是Oracle时,如果插入的数据是空时,必须是要指定默认的参数。所以这里的设置要这么写:jdbcType=VARCHAR。 当然mysql数据库是不用jdbcType=VARCHAR这么写的。 --> <insert id="insert" parameterType="cn.itcast.jk.domain.Factory"> insert into factiry_c(FACTORY_ID,FULL_NAME,CONTACTS,PHONE,MOBILE,FAX,CNOTE,INSPECTOR) value( #{id,jdbcType=VARCHAR}, #{fullName,jdbcType=VARCHAR}, #{factoryName,jdbcType=VARCHAR}, #{contacts,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR}, #{fax,jdbcType=VARCHAR}, #{cnote,jdbcType=VARCHAR}, #{inspector,jdbcType=VARCHAR}, #{orderNo,jdbcType=INTEGER}, #{createBy,jdbcType=VARCHAR}, #{createDept,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP} ) </insert> </mapper>
3 整合springmvc;编写springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 1. 包扫描 controller
这样就可以把控制器类扫描生成。控制器。
--> <context:component-scan base-package="cn.itcast.jk.controller"/> <!-- 2. 内部资源视图解析器 --> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages"/> <property name="suffix" value=""/> </bean> </beans>
上面的spring配好了。mybatis也配好了。springmvc也配好了,最后一步就是把三者整合整合起来,这个一个web项目,当然在web.xml中写。
web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Archetype Created Web Application</display-name> <!-- 1.spring框架 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--2. springmvc servlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc/springmvc-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- 乱码的解决 --> <filter> <filter-name>SpringEncoding</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>SpringEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
总结:spring和springmvc的配置在web.xml中配置,mybatis的配置在spring的beans.xml中配置。