mybatis的简单应用

app--|---->mubatis--————————————————-|--->jdbc------>

     映射关系(ORM)Xxmapper.xml                连接信息(mybatis-config.xml)

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>
        <typeAlias type="cn.itcast.mybatis.domain.User" alias="aliasesUser"></typeAlias>
    </typeAliases>
<environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="friends"/> </dataSource> </environment> <!-- 产品 可用于多个配置的 --> <environment id="product"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="friends"/> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/itcast/mybatis/domain/MyUser.xml"/> </mappers> </configuration>

 usermapper.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.mybatis.domain.User">
    <sql id="cols">
        id,name,age,address
    </sql>
    <sql id="ucols">
        name=#{name},age=#{age},address=#{address}
    </sql>

    <!-- 查询所有记录 -->
    <select id="listAll" resultType="aliasesUser">
        select <include refid="cols"/> from user_c
    </select>
    
    <!-- 按条件查询 -->
    <select id="find" parameterType="aliasesUser" resultType="aliasesUser">
        select * from user_c 
        <where>
        <if test="name!=null">
            and name like "%"#{name}"%"
        </if>
        <if test="age!=null">
            and age=#{age}
        </if>
        </where>
    </select>
    
    <!-- 查询一个用户 -->
    <select id="get" parameterType="string" resultType="aliasesUser">
        select <include refid="cols"/> from user_c where id=#{id}
    </select>
    
    <!-- 新增 -->
    <insert id="create" parameterType="aliasesUser">
        insert into user_c (id,name,age,address) values(#{id},#{name},#{age},#{address})
    </insert>
    
    <!-- 修改 -->
    <update id="update" parameterType="aliasesUser">
        update user_c
         <set>
             <if test="name!=null">
                 name=#{name},
             </if>
             <if test="age!=null">
                 age=#{age},
             </if>
             <if test="address!=null">
                 address=#{address},
             </if>
         </set>
         
        where id=#{id}
    </update>
    
    <!-- 删除 -->
    <delete id="delete" parameterType="string">
        delete from user_c where id=#{id}
    </delete>
</mapper>

 参数:1.对象#{属性名}

           2.map:#{key}

   3.多个参数#{0},#{1}...

返回结果如果是list,resultType=list里的元素类型

<insert>需要主键:

<insert id="add" parameterType="EStudent" useGeneratedKeys="true" keyProperty="id" keycolumn="id">
  insert into TStudent(name, age) values(#{name}, #{age})
</insert>

 多表查询

方法一:

<resultMap type="me.gacl.domain.Classes" id="ClassResultMap">
        <id property="id" column="c_id"/>
        <result property="name" column="c_name"/>
        <association property="teacher" javaType="me.gacl.domain.Teacher">
            <id property="id" column="t_id"/>
            <result property="name" column="t_name"/>
        </association>
    </resultMap>

 方法二:

<resultMap type="me.gacl.domain.Classes" id="ClassResultMap2">
        <id property="id" column="c_id"/>
        <result property="name" column="c_name"/>
        <association property="teacher" column="teacher_id" select="getTeacher"/>
     </resultMap>
     
     <select id="getTeacher" parameterType="int" resultType="me.gacl.domain.Teacher">
        SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
     </select>

 方法三:

<association property="属性名" javaType="Dept" column="关联字段"(可以不加) resultMap="dao.deptDao.deptMapper(namespace)"现在dept自己的mapper中写好映射

 集合:

 <collection property="students" ofType="me.gacl.domain.Student">
            <id property="id" column="s_id"/>
            <result property="name" column="s_name"/>
        </collection>

 外部mapper加入

<collection property="‘ ofType="" resultMap="...">

 spring-mybatis.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-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/tx
                         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                         http://www.springframework.org/schema/aop 
                         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
    <!-- 数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost/mybatisdb?characterEncoding=utf-8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="friends"></property>
    </bean>
    
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
        <property name="dataSource" ref="dataSource"></property>

    </bean>
    
    <!-- 事务相关控制 -->
    <bean name="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 通知 -->
    <tx:advice id="tx"
        transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="get*" read-only="true" />
            <tx:method name="select*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="pc" expression="execution(* cn.itcast.mybatis.service.*.*(..))" />
        <!--把事务控制在Service层-->
        <aop:advisor pointcut-ref="pc" advice-ref="tx" />
    </aop:config>
    
    <bean id="userDao" class="cn.itcast.mybatis.dao.UserDaoImpl">
        <property name="sqlSessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <bean id="userService" class="cn.itcast.mybatis.service.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>
    
    
    <bean id="userAction" class="cn.itcast.mybatis.action.UserAction" scope="prototype">
        <property name="userService" ref="userService">
<!--         一样    <ref bean="userService"/> -->
        </property>
    </bean>
</beans>

 

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.hua.saf" />
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}" />
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}" />
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}" />
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}" />
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}" />
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation"  value="classpath:sqlMapConfig.xml"/>
        <!-- 自动扫描mapping.xml文件,**表示迭代查找,也可在sqlMapConfig.xml中单独指定xml文件-->
        <property name="mapperLocations" value="classpath:com/hua/saf/**/*.xml" />
    </bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.zte.ccs.os.meeting.**.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

 

<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>

 

posted @ 2017-10-11 16:09  zmoony  阅读(130)  评论(0)    收藏  举报