zoukankan      html  css  js  c++  java
  • MyBatis + spring 增删改查

    1 先来看 dao.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="spring.mybatis.Dao">
     
    <!-- 查1 --> 
    <select id="queryName" parameterType="int" resultType="string">
    select name from stu where id = #{id}
    </select>
     
    <!-- 查2 返回类型为 resultMap-->
    <resultMap type="spring.mybatis.Bean" id="myMapBean">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="sex" column="sex"/>
    <result property="birs" column="birs"/>
    <result property="message" column="message"/>
    </resultMap>
    <select id="quaryObjectsByMap1" resultMap="myMapBean">
    select * from stu
    </select>
     
    <!-- 查3 返回类型为 Collection-->
    <resultMap type="spring.mybatis.Bean" id="myMapCollection">
    <!-- 这种方式查的默认是 property 和 column  是名字相同的,不同的返回null -->
    <collection property="beans" 
    javaType="ArrayList" 
    ofType="Bean"
    select="selectBean"></collection>
    </resultMap>
    <select id="quaryObjectsByMap2" resultMap="myMapCollection">
    select * from stu
    </select>
     
    <!-- 查4 传入参数为:parameterType="Map"   查5 传入参数为: parameterType="Bean"-->
     
    <!-- 普通增 -->
    <insert id="addStu">
    insert into stu(id,name,sex)values(#{id},#{name},#{sex})
    </insert>
     
    <!-- 带自增列的增 -->
    <insert id="sequenceAdd">
    <selectKey keyProperty="id"  resultType="java.lang.Integer" order="BEFORE">
    select seq_stu.nextval as id from dual<!-- oracle中  序列可以不用绑定触发器 -->
    </selectKey>
    insert into stu(id,name,sex)values(#{id},#{name},#{sex})
    </insert>

    <!-- 更正:自增列里需要返回值 ,才能插入 ,这里还牵扯到要在插入前执行selectKey 还是在插入后执行selectKey-->

    <!-- 通常这么写-->

    <selectKey keyProperty="pcid" resultType="java.lang.Integer" order="BEFORE">
     select seq_wjpc.nextval as pcid from dual
    </selectKey> 

    要获取插入数据的ID 值,用传入对象.getId()  或者Map.get("id")来获取

     
    <!-- 插入clob 对象 的增-->
    <insert id="clobAdd">
    insert into stu(name,sex,birS,message)values(
    #{name,jdbcType=VARCHAR},<!-- 最好都大写(NUMERIC,DATE,TIME,BLOB,CLOB,NVARCHAR,CHAR,CURSOR) -->
    #{sex,jdbcType=VARCHAR},
    #{bir,jdbctype=DATE},
    #{message,jdbcType=CLOB}
    )
    </insert>
    <!--保存年月日 用DATE,保存年月日时分秒 用TIMESTAMP -->
    <!-- 删1 -->
    <delete id="delById" parameterType="int">
    delete from stu where id = #{id}
    </delete>
     
    <!-- 改1 -->
    <update id="updateStu">
    update stu set name=#{newName} where id= #{id}
    </update>
     
    <!-- 调用存储过程 -->

    <!-- 1返回值是游标--> 

    <select id="procedureCall" statementType="CALLABLE">
    <!--为了减少不必要的麻烦,最好遵循命名规范用jdbcType,区分大小写 ,out中的javaType此处必不可少  -->
    call pac_stu.getstu(#{input,mode=IN,jdbcType=NUMERIC,javaType=Integer}, 
     #{stus,mode=OUT,jdbcType=CURSOR,resultMap=myMapBean,javaType=java.sql.ResultSet}) 
    </select>
         <!-- 返回游标时,resultMap 指定容纳游标单条数据的对应对象 -->

     <!-- 2返回值是普通数据类型 -->

    call pac_random.sjcc( #{ccxh,mode=OUT,jdbcType=VARCHAR,resultType=String})

    <!-- 不管返回的是什么数据类型,都应该传map类型的参数,取得返回值的时候才能map.get(key)来获得返回值-- > 

    </mapper>

    2:再来看 和spring整合后,对应的接口写法:

    public interface Dao {
    //查询姓名根据id
    public String queryName(@Param(value = "id")int id);
     
    //查1
    public List<Bean> quaryObjectsByMap1();
     
    //查2
    public List<Bean> quaryObjectsByMap2();
     
    //1-增
    public int addStu(@Param("id")int id,@Param("name") String name,@Param("sex") String sex);
     
    //2-Sequence增
    public int sequenceAdd(@Param("id")int id,@Param("name") String name,@Param("sex") String sex);
     
    //3-Colb增
    public int clobAdd(@Param("name") String name,@Param("sex") String sex,@Param("bir")Date date,@Param("message")String message);
     
    //1-删
    public int delById(@Param("id")int id);
     
    //1-改
    public int updateStu(@Param(value="newName")String newName,@Param(value="id")int id);
     
    //1-调用存储过程
    public List<Bean> procedureCall(Map map);
    }

    3:最后来看 在service 中做的测试引用:

    public class Service {
    private Dao dao;
     
    public static void main(String[] args) {
    ApplicationContext ac = 
    new ClassPathXmlApplicationContext("classpath:/spring/mybatis/config/spring_applactionContext.xml");
    Dao dao = (Dao)ac.getBean("dao");
    //String name = dao.queryName(4);
    //System.out.println(name);
    //int id = 0;
    //dao.addStu(4, "", "女");
    //dao.addStu(id, "ss","nv");
    //List<Bean> beans = dao.quaryObjectsByMap2();
    //for(Bean bean:beans){
    //System.out.println(bean.toString());
    //}
    //System.out.println("??");
    //int i =dao.delById(3);
    //System.out.println(i + "****");
    //dao.clobAdd("zzd", "M",new Date(), "this is clob");
    //dao.updateStu("zzd", 4);
     
    //可行
    Map map = new HashMap<String, Object>();
    map.put("input", 1);
    dao.procedureCall(map);
    List list = (List)map.get("stus");
    System.out.println(list.size());
     
    /*行不通 换成注解方式
    * List list = new ArrayList();
    dao.procedureCall(1, list);
    System.out.println(list.size());*/
    }

     4:最后是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:tx="http://www.springframework.org/schema/tx" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
            
    <!-- 配置数据源 -->
    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
    <value>oracle.jdbc.driver.OracleDriver</value>
    </property>
    <property name="url">
    <value>jdbc:oracle:thin:@localhost:1521:orcl</value>
    </property>
    <property name="username">
    <value>ceshi</value>
    </property>
    <property name="password">
    <value>oracle</value>
    </property>
    </bean>
     
    <!-- 配完数据源 和 拥有的 sql映射文件 sqlSessionFactory 也可以访问数据库 和拥有 sql操作能力了-->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
     <property name="configLocation" value="classpath:/spring/mybatis/config/mybatis_config.xml"></property> 
    <property name="dataSource" ref="dataSource" /> 
     </bean>
     

    今天刚发现这个地方可以改一下,可以不用要mybatis_config.xml 文件了:

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <property name="dataSource" ref="dataSource" />
     <property name="mapperLocations"> 

    <list>
     <value>classpath:com/azcsoft/dao/TestDao.xml</value>
     </list>
     </property>
         </bean>

    <!-- 通过设置 mapperInterface属性,使接口服务bean 和对应xml文件管理 可以使用其中的sql -->
    <bean id="dao" class="org.mybatis.spring.mapper.MapperFactoryBean" >
    <!-- 此处等同于 Mybatis 中 ServerDao serverDao = sqlSession.getMapper(ServerDao.class); 指明映射关系-->
    <property name="mapperInterface" value="spring.mybatis.Dao" /> 
    <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
    </bean> 
     
    </beans>
  • 相关阅读:
    C++中整型变量的存储大小和范围
    A1038 Recover the Smallest Number (30 分)
    A1067 Sort with Swap(0, i) (25 分)
    A1037 Magic Coupon (25 分)
    A1033 To Fill or Not to Fill (25 分)
    A1070 Mooncake (25 分)
    js 获取控件
    C#代码对SQL数据库添加表或者视图
    JS 动态操作表格
    jQuery取得下拉框选择的文本与值
  • 原文地址:https://www.cnblogs.com/leonkobe/p/3300134.html
Copyright © 2011-2022 走看看