关闭

spring mybatis整合

91人阅读 评论(0) 收藏 举报
分类:
<?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.liyang.mapper.WebSiteMapper">

	<resultMap id="BaseResultMap" type="com.liyang.entity.WebSiteEntity2">
		<id column="id" property="idd" jdbcType="INTEGER" />
		<result column="web_name" property="webName" jdbcType="VARCHAR" />
		<result column="web_url" property="webUrl" jdbcType="VARCHAR" />
		<result column="md5" property="md5" jdbcType="VARCHAR" />
		<result column="updated" property="updated" jdbcType="VARCHAR" />
		<result column="update_time" property="updateTime" jdbcType="DATE" />
		<result column="last_update" property="lastUpdate" jdbcType="DATE" />
	</resultMap>

	<sql id="Base_Column_List">
		id, web_name, web_url, md5, updated, update_time, last_update
	</sql>

	<select id="getWebSite" resultMap="BaseResultMap" parameterType="java.lang.Long">
		select
		<include refid="Base_Column_List" />
		from website
		where id = #{id,jdbcType=BIGINT}
	</select>


	<select id="getWebSiteList" resultMap="BaseResultMap" parameterType="java.lang.Long">
		select
		<include refid="Base_Column_List" />
		from website
		where id = #{id,jdbcType=BIGINT}
	</select>


	<insert id="addWebSite" parameterType="com.liyang.entity.WebSiteEntity2">
		insert into website
		(id , web_name , web_url , md5 , updated , update_time , last_update)
		values
		(
		#{idd , jdbcType = INTEGER} ,
		#{webName, jdbcType = VARCHAR} ,
		#{webUrl, jdbcType = VARCHAR} ,
		#{md5, jdbcType = VARCHAR} ,
		#{updated, jdbcType = VARCHAR} ,
		#{updateTime, jdbcType = DATE} ,
		#{lastUpdate, jdbcType = DATE}
		)
	</insert>


	<insert id="addWebSiteSelective" parameterType="com.liyang.entity.WebSiteEntity2">
		insert into website
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="idd != null">
				id ,
			</if>
			<if test="webName != null">
				web_name ,
			</if>
			<if test="webUrl != null">
				web_url ,
			</if>
			<if test="md5 != null">
				md5 ,
			</if>
			<if test="updated != null">
				updated ,
			</if>
			<if test="updateTime != null">
				update_time ,
			</if>
			<if test="lastUpdate != null">
				last_update ,
			</if>
		</trim>

		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="idd != null">
				#{idd , jdbcType = INTEGER} ,
			</if>
			<if test="webName != null">
				#{webName, jdbcType = VARCHAR} ,
			</if>
			<if test="webUrl != null">
				#{webUrl, jdbcType = VARCHAR} ,
			</if>
			<if test="md5 != null">
				#{md5, jdbcType = VARCHAR} ,
			</if>
			<if test="updated != null">
				#{updated, jdbcType = DATE} ,
			</if>
			<if test="updateTime != null">
				#{updateTime, jdbcType = DATE} ,
			</if>
			<if test="lastUpdate != null">
				#{lastUpdate, jdbcType = DATE} ,
			</if>
		</trim>

	</insert>

	<select id="getInfo" resultType="java.util.HashMap" parameterType="java.lang.Long">
		select web_name as A , web_url as B , update_time
		from website
		where id = #{id,jdbcType=BIGINT}
	</select>


	<select id="getWebSiteEntitySize" resultType="java.lang.Integer">
		select count(1)
		from website
	</select>

	<select id="getMlist" resultType="java.util.HashMap">
		select web_name as A , web_url as B , update_time
		from website
	</select>

	<select id="getUrls" resultType="java.lang.String">
		select web_url
		from website
	</select>

	<select id="getUrlss" resultType="java.lang.String">
		select web_url
		from website
		where id in
		<foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
			#{id}
		</foreach>
	</select>

	<select id="getIds" resultType="java.lang.Integer">
		select id
		from website
	</select>

</mapper>

package com.liyang.mapper;

import java.util.List;
import java.util.Map;

import com.liyang.entity.WebSiteEntity2;

public interface WebSiteMapper {
	public List<WebSiteEntity2> getWebSiteList(Long id) ; 

    WebSiteEntity2 getWebSite(Long id) ;
    
    List<Map<String, Object>> getMlist();
    
    
    void  addWebSite(WebSiteEntity2 webSite) ;
    
    Integer getWebSiteEntitySize() ;

    void  addWebSiteSelective(WebSiteEntity2 webSite) ;
    
    Map<String , Object> getInfo(Long id) ;
 
    List<String> getUrls() ;
    
    List<String> getUrlss(List<Integer> o) ;
    
    List<Integer> getIds() ;
}


package com.liyang;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.liyang.entity.WebSiteEntity2;
import com.liyang.mapper.WebSiteMapper;

public class SpringContainerByMybatis {
	public static Logger LOG = LoggerFactory.getLogger(SpringContainer.class) ;

	@Autowired
	public static ApplicationContext context;

	@Autowired
	public static WebSiteMapper mapper ; 
	

	public static void main(String[] args) {
		String[] configLocations = { "applicationContext-resource.xml",
									 "applicationContext-jdbc.xml"} ;
		
		context = new ClassPathXmlApplicationContext(configLocations);
		
		mapper = context.getBean(WebSiteMapper.class) ;
		
		LOG.info("begin") ;
		
		List<WebSiteEntity2> res = mapper.getWebSiteList(1L)  ; 
	/*	Integer res = mapper.getWebSiteEntitySize() ;*/
		System.out.println(res) ;
		LOG.info("end") ;
	}
}

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
	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:tx="http://www.springframework.org/schema/tx"
	xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
	<!-- 配置数据库事务 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 配置Mybatis的拦截器 -->
	<!-- <bean id="paginationInterceptor" class="com.pingan.ebd.api.db.interceptor.PaginationInterceptor" />
 -->
	<!-- 配置Mybatis的SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="mybatis.cfg.xml" />
		<property name="mapperLocations" value="com/liyang/**/*Mapper.xml" />
		<!-- <property name="plugins">
			<array>
				<ref bean="paginationInterceptor" />
			</array>
		</property> -->
	</bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
		<property name="basePackage" value="com.liyang.mapper" />
	</bean>
	
	<!-- 配置Spring JdbcTemplate -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
	</bean>

</beans>

<?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>
		<!-- changes from the defaults -->
		<setting name="lazyLoadingEnabled" value="false" />
		<!-- 表名重用预编译的SQL语句 -->
		<setting name="defaultExecutorType" value="REUSE" />
		<!-- 全局映射器启用缓存 -->
		<setting name="cacheEnabled" value="true" />
		<!-- 查询时,关闭关联对象即时加载以提高性能 -->
		<setting name="lazyLoadingEnabled" value="true" />
		<!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
		<setting name="aggressiveLazyLoading" value="false" />
		<!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
		<setting name="multipleResultSetsEnabled" value="true" />
		<!-- 允许使用列标签代替列名 -->
		<setting name="useColumnLabel" value="true" />
		<!-- 允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->
		<setting name="useGeneratedKeys" value="true" />
		<!-- 给予被嵌套的resultMap以字段-属性的映射支持 -->
		<setting name="autoMappingBehavior" value="FULL" />
		<!-- 对于批量更新操作缓存SQL以提高性能 -->
		<setting name="defaultExecutorType" value="SIMPLE" />
		<!-- 数据库超过25000秒仍未响应则超时 -->
		<setting name="defaultStatementTimeout" value="25000" />
	</settings>

	<!-- 别名定义 -->
	<typeAliases>
	</typeAliases>
	
<!-- 	<mappers>  
         <mapper resource="com/liyang/mapper/WebSiteMapper.xml"/>  
    </mappers>  -->

</configuration> 




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:232019次
    • 积分:9846
    • 等级:
    • 排名:第1877名
    • 原创:680篇
    • 转载:0篇
    • 译文:0篇
    • 评论:55条
    文章分类
    最新评论