zoukankan      html  css  js  c++  java
  • 14.

    1.Spring和MyBatis整合
    	1)导入spring-mybatis.jar
    	2)整合
    		a)org.mybatis.spring.SqlSessionFactoryBean
    			1)dataSource
    			2)myBatis的配置文件
    	3)事务管理器用DataSourceTransctionManager
    	4)dao层
    		1)SqlSessoinTemplate
    		2)SqlSessionDaoSupport
    	5)自动生成dao层的代理类
    		1)org.mybatis.spring.mapper.MapperScannerConfigurer
    			1)接口的全全类名
    			2)配置sqlSessionFactory名字 

    开发一个全新的项目

    1、导入jar

    Mybatisjar

    asm-4.2.jar

    cglib-3.1.jar

    commons-logging-1.2.jar

    log4j-1.2.17.jar

    log4j-api-2.2.jar

    log4j-core-2.2.jar

    mybatis-3.3.0.jar

    mysql-connector-java.jar

    slf4j-api-1.7.12.jar

    slf4j-log4j12-1.7.12.jar

    Spring+SpringMVC+ORM

    spring-beans-3.2.0.RELEASE.jar

    spring-context-3.2.0.RELEASE.jar

    spring-core-3.2.0.RELEASE.jar

    spring-expression-3.2.0.RELEASE.jar

    spring-jdbc-3.2.0.RELEASE.jar

    spring-orm-3.2.0.RELEASE.jar

    spring-test-3.2.0.RELEASE.jar

    spring-web-3.2.0.RELEASE.jar

    spring-webmvc-3.2.0.RELEASE.jar

    aop+tx

    com.springsource.org.aopalliance-1.0.0.jar

    com.springsource.org.aspectj.tools-1.6.6.RELEASE.jar

    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

    spring-aop-3.2.0.RELEASE.jar

    spring-aspects-3.2.0.RELEASE.jar

    spring-tx-3.2.0.RELEASE.jar

    jackson

    jackson-annotations-2.5.4.jar

    jackson-core-2.5.4.jar

    jackson-databind-2.5.4.jar

    springmybatis整合需要的jar

    mybatis-spring-1.2.0.jar

    c3p0连接池

    com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar

    文件上传

    com.springsource.org.apache.commons.fileupload-1.2.0.jar

    com.springsource.org.apache.commons.io-1.4.0.jar

    2、导入相关的配置文件

    3、关键配置

    (1) web.xml:配置过滤器,监听器,核心控制器,

       

            <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>
    
    
    	<!-- 确定Spring配置文件的位置 -->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:applicationContext.xml</param-value>
    	</context-param>
    
    
    	<!-- 在Web容器启动的时候初始化Spring容器 -->
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    
    	<servlet>
    		<servlet-name>springmvc</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    
    		<!-- 指定SpringMVC的配置文件路径 -->
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>classpath:springmvc-servlet.xml</param-value>
    		</init-param>
    
    		<!-- 加载servlet优先级,数值越小优先级越高 -->
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>springmvc</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    

     

      

     

     

    2springmvc.xml:

    1.指定需要扫描的控制器

    2.配置视图解析器

    3.文件上传解析器

    4.json解析器

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    	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/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
            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">
    	<!-- 开启注解驱动 -->
    	<mvc:annotation-driven />
    
    	<!-- 忽略静态资源 -->
    	<mvc:resources location="/js/*" mapping="/js/**"></mvc:resources>
    	
    	<!-- 开启包扫描 -->
    	<context:component-scan base-package="com.qf"></context:component-scan>
    
    	<!-- 配置视图解析器 -->
    	<bean id="jspViewResolver"
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<!-- 用jstl事务解析器 -->
    		<property name="viewClass"
    			value="org.springframework.web.servlet.view.JstlView" />
    		<!-- 将视图名 渲染后视图的前缀 -->
    		<property name="prefix" value="/" />
    
    		<!-- 渲染后视图的后缀 -->
    		<property name="suffix" value=".jsp" />
    	</bean>
    	
    	<!-- 文件上传配置 -->
    	<bean id="multipartResolver"
    		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    		<!-- 设置编码格式 -->
    		<property name="defaultEncoding" value="utf-8"></property>
    		<!-- 设置文件大小 -->
    		<property name="maxUploadSize" value="20971520"></property>
    	</bean>
    	
    	<!-- 配置支持JSON格式的转换器 -->
    	<bean id="jsonConverter"
    		class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
    
    	<bean
    		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    		<property name="messageConverters">
    		    <list>
    			<ref bean="jsonConverter" />
    		    </list>
    		</property>
    	</bean>
    </beans> 

    3applicationContext.xml:

                      开启包扫描

    配置数据源,

    整合mybatis,

    扫描Service

    Dao(采用注解方式实现依赖)

    配置事务管理器

    事务的策略

    事务范围

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    	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/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
            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">
    
    	<!-- 注解实现必须要开启包扫描 -->
    	<context:component-scan base-package="com.qf"></context:component-scan>
    	
    	<context:property-placeholder location="classpath:jdbc.properties" />
    
    	<!-- 实例化数据源 -->
    	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    		<!-- ${属性文件中的key} -->
    		<property name="user" value="${jdbc.name}"></property>
    		<property name="password" value="${jdbc.password}"></property>
    		<property name="jdbcUrl" value="${jdbc.url}"></property>
    		<property name="driverClass" value="${jdbc.driver}"></property>
    	</bean>
    
    	<!-- Spring整合MyBatis -->
    	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    		<!-- 数据源 -->
    		<property name="dataSource" ref="dataSource"></property>
    		<!-- 指定MyBatis配置文件 -->
    		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
    		
    	</bean>
    	
    	<!-- 定义事务管理器 -->
    	<bean id="tx"
    		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="dataSource"></property>
    	</bean>
    
    	<!-- 事务的策略 -->
    	<tx:advice id="txAdvice" transaction-manager="tx">
    		<tx:attributes>
    			<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" />
    			<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" />
    			<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" />
    			<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" />
    			<tx:method name="create*" isolation="DEFAULT" propagation="REQUIRED" />
    			
    			
    			<tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED"  read-only="true"/>
    			<tx:method name="get*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
    			<tx:method name="select*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
    				
    		</tx:attributes>
    	</tx:advice>
    	
        <!-- 事务范围 -->
    	<aop:config>
    		<aop:advisor advice-ref="txAdvice"
    			pointcut="execution(* com.qf.service..*.*(..))" />
    	</aop:config>
    
    </beans>

    (4)spring整合mybatis

       导入mybatis-spring-1.2.0.jar

              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>
    
    	<typeAliases>
    		<package name="com.qf.entity"/>
    	</typeAliases>
    	
    </configuration>

    0.数据库

    1.创建项目

    2.建包建配置文件

       com.qf.entity

       com.qf.dao

       com.qf.mapper

       com.qf.utils

    mybatis-config.xml  

    3.con.qf.entity包中创建实体类

    package com.qf.entity;
    
    import java.security.KeyStore.PrivateKeyEntry;
    
    public class User {
    
    	private Integer id;
    	
    	private String name;
    	
    	private String password;
    
    	
    

      

    package com.qf.entity;
    
    public class Account {
    
    	private Integer id;
    	
    	private String name;
    	
    	private Double balance;
    	
    	private String inName;
    	
    	private String outName;
    	
    	private Double money;
    
    }
    

     

    package com.qf.entity;
    
    import java.util.List;
    
    public class Page {
    	/**
    	 * 当前页
    	 */
    	private Integer currentPage = 1;
    
    	/**
    	 * 每页显示的行数
    	 */
    	private Integer pageSize = 3;
    
    	/**
    	 * 总页数
    	 */
    	private Integer totalPage;
    
    	/**
    	 * 总条数
    	 */
    	private Integer totalCount;
    
    	/**
    	 * 展示数据
    	 */
    	private List<?> list;
    
    	/**
    	 * 查询路径
    	 */
    	private String url;
    
    	
    	public void setTotalCount(Integer totalCount) {
    		this.totalCount = totalCount;
    		this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount/ pageSize) + 1;
    	}
    
    }
    

      

    4.con.qf.dao包中创建接口

    公共接口

    package com.qf.dao;
    
    import java.util.List;
    
    public interface IBaseDao<T> {
    
    	public int add(T t);
    	
    	public int update(T t);
    	
    	public int delete(Integer ID);
    	
    	public T getById(Integer id);
    	
    	public int getCount();
    	
    	public List<T> getList(Integer startIndex,Integer size);
    }
    

      

    package com.qf.dao;
    
    import com.qf.entity.User;
    
    public interface IUserDao extends IBaseDao<User>{
    
    }
    

      

    package com.qf.dao;
    
    import com.qf.entity.Account;
    
    public interface IAccountDao extends IBaseDao<Account> {
    
    	/**
    	 * 进账
    	 * @param name 用户名
    	 * @param moeny 金额
    	 */
    	public void in(String name,Double moeny);
    	
    	
    	/**
    	 * 出账
    	 * @param name 用户名
    	 * @param moeny 金额
    	 */
    	public void out(String name,Double moeny);
    }
    

      

    5.con.qf.dao.impl包创建dao层的实现层

    公共实现类

    package com.qf.dao.impl;
    
    import java.lang.reflect.ParameterizedType;
    import java.lang.reflect.Type;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionTemplate;
    import org.mybatis.spring.support.SqlSessionDaoSupport;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.qf.dao.IBaseDao;
    
    public class BaseDaoImpl<T> extends SqlSessionDaoSupport implements IBaseDao<T> {
    
    	private Class<T> entity;
    	
    	public BaseDaoImpl() {
    		ParameterizedType parameterizedType = (ParameterizedType) this.getClass().getGenericSuperclass();
    
    		Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
    
    		entity = (Class<T>) actualTypeArguments[0];
    	}
    
    	@Override
    	public int add(T t) {
    		return getSqlSession().insert("com.qf.dao.I" + entity.getSimpleName() + "Dao.add", t);
    	}
    
    	@Override
    	public int update(T t) {
    		return getSqlSession().update("com.qf.dao.I" + entity.getSimpleName() + "Dao.update", t);
    	}
    
    	@Override
    	public int delete(Integer id) {
    		return getSqlSession().delete("com.qf.dao.I" + entity.getSimpleName() + "Dao.delete", id);
    	}
    
    	@Override
    	public T getById(Integer id) {
    		return getSqlSession().selectOne("com.qf.dao.I" + entity.getSimpleName() + "Dao.getById", id);
    	}
    
    	@Override
    	public int getCount() {
    		return getSqlSession().selectOne("com.qf.dao.I" + entity.getSimpleName() + "Dao.getCount");
    	}
    
    	@Override
    	public List<T> getList(Integer startIndex, Integer size) {
    		Map<String, Object> map = new HashMap<String, Object>();
    		map.put("startIndex", startIndex);
    		map.put("size", size);
    		return getSqlSession().selectList("com.qf.dao.I" + entity.getSimpleName() + "Dao.getList", map);
    	}
    
    }
    

      

    package com.qf.dao.impl;
    
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    
    import com.qf.dao.IUserDao;
    import com.qf.entity.User;
    
    @Repository
    public class UserDaoImpl extends BaseDaoImpl<User> implements IUserDao {
    
    	@Autowired
    	@Override
    	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
    		super.setSqlSessionFactory(sqlSessionFactory);
    	}
    
    }
    

      

    package com.qf.dao.impl;
    
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    
    import com.qf.dao.IAccountDao;
    import com.qf.entity.Account;
    
    @Repository
    public class AccountDaoImpl extends BaseDaoImpl<Account> implements IAccountDao {
    
    	@Override
    	public void in(String name, Double moeny) {
    		
    	}
    	@Override
    	public void out(String name, Double moeny) {
    		
    	}	
    	@Autowired
    	@Override
    	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
    		super.setSqlSessionFactory(sqlSessionFactory);
    	}
    
    }
    

      

    6.con.qf.biz包中创建接口

    公共接口

    7.con.qf.biz.impl包创建biz层的实现层

    公共实现类

    8.com.qf.contrlller包

    9.配置jdbc.properties配置文件

    10配置mybatis-config.xml配置文件

    11.com.qf.mapper包创建接口的映射文件

    12.在mybatis-config.xml里面读取Mapper文件

    13.com.qf.util工具类

    14.页面

     

  • 相关阅读:
    matlab代码学习_2018728
    理解面向对象过程中创造一个对象的步骤
    html字符及空格占位问题
    div中img(及文字等inline元素和block元素)的自动换行
    css布局居中
    css3前缀解决方案
    自己写一个树形导航菜单
    《JavaScript核心概念及实践》 读书笔记
    css常用属性
    windows下有个目录名称中间有空格 java读目录空格变成%20 处理方法
  • 原文地址:https://www.cnblogs.com/tournesol/p/8099430.html
Copyright © 2011-2022 走看看