zoukankan      html  css  js  c++  java
  • mybatis总结(二)

    实现简单的crud,上一节已经进行最简单的查询

    这次在oracle数据库中操作,就在oracle实例中操作,分别是Dept表和Emp表,所以配置文件需要改动。

    1.实体类

    Dept类

    public class Dept {
        private int deptno;//部门编号
        private String dname;//部门名称
        private String loc;//所在地区
        private List<Emp> emps;//一对多关系
        
        
        public List<Emp> getEmps() {
            return emps;
        }
        public void setEmps(List<Emp> emps) {
            this.emps = emps;
        }
        public int getDeptno() {
            return deptno;
        }
        public void setDeptno(int deptno) {
            this.deptno = deptno;
        }
        public String getDname() {
            return dname;
        }
        public void setDname(String dname) {
            this.dname = dname;
        }
        public String getLoc() {
            return loc;
        }
        public void setLoc(String loc) {
            this.loc = loc;
        }
        
    }

    Emp类(只取几个字段,并没有把所有的列转换过来)

    public class Emp {
    	private int empno;//员工编号
    	private String ename;//员工姓名
    	private Dept dept;//部门对象
    	private String job;//员工工作
    	
    	public String getJob() {
    		return job;
    	}
    	public void setJob(String job) {
    		this.job = job;
    	}
    	public int getEmpno() {
    		return empno;
    	}
    	public void setEmpno(int empno) {
    		this.empno = empno;
    	}
    	public String getEname() {
    		return ename;
    	}
    	public void setEname(String ename) {
    		this.ename = ename;
    	}
    	public Dept getDept() {
    		return dept;
    	}
    	public void setDept(Dept dept) {
    		this.dept = dept;
    	}
    }
    

    2.数据库连接文件 db.properties

    driver=oracle.jdbc.driver.OracleDriver//驱动名
    url=jdbc:oracle:thin:@localhost:1521:MLDN//地址名
    username=scott//用户名
    password=tiger//密码

    3.编写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>
    
    	<!-- properties属性配置文件的引入 -->
    	<properties resource="db.properties"></properties>
    	<!-- setting设置,调整mybaits运行行为 -->
    	<settings>
    	  <!-- 匹配下划线到驼峰式命名法 -->
    	  <setting name="mapUnderscoreToCamelCase" value="true"/>
    	  <setting name="safeRowBoundsEnabled" value="false"/>
    	  <setting name="cacheEnabled" value="true"/>
    	  <setting name="lazyLoadingEnabled" value="true"/>
    	  <setting name="multipleResultSetsEnabled" value="true"/>
    	  <setting name="useColumnLabel" value="true"/>
    	  <setting name="useGeneratedKeys" value="false"/>
    	  <setting name="autoMappingBehavior" value="PARTIAL"/>
    	  <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
    	  <setting name="defaultExecutorType" value="SIMPLE"/>
    	  <setting name="defaultStatementTimeout" value="25"/>
    	  <setting name="defaultFetchSize" value="100"/>
    	  <setting name="localCacheScope" value="SESSION"/>
    	  <setting name="jdbcTypeForNull" value="OTHER"/>
    	  <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
    	</settings>
    	<!-- 类起别名 -->
    	<typeAliases>
    		<!-- <typeAlias type="com.qyxy.entity.Dept" alias="Dept"/> -->
    		<package name="com.qyxy.entity"/>
    	</typeAliases>
    	<!-- 设置要使用的数据库环境 -->
    	<environments default="development">//选择mysql配置
    	
    		<environment id="development">
    			<transactionManager type="JDBC" />
    			<dataSource type="POOLED">
    				<property name="driver" value="${driver}" />
    				<property name="url" value="${url}" />
    				<property name="username" value="${username}" />
    				<property name="password" value="${password}" />
    			</dataSource>
    		</environment>
    			
    		<environment id="mysql">
    			<transactionManager type="JDBC" />
    			<dataSource type="POOLED">
    				<property name="driver" value="${jdbc.driverClassName}" />
    				<property name="url" value="${jdbc.url}" />
    				<property name="username" value="${jdbc.username}" />
    				<property name="password" value="${jdbc.password}" />
    			</dataSource>
    		</environment>
    		
    	</environments>
    
    	<!-- 定义不同的数据库厂商标识 -->
    	<databaseIdProvider type="DB_VENDOR">
    	  <property name="SQL Server" value="sqlserver"/>
    	  <property name="DB2" value="db2"/>        
    	  <property name="Oracle" value="oracle" />
    	  <property name="MySql" value="mysql" />
    	</databaseIdProvider>
    
    	<!-- 映射的sql文件 -->
    	<mappers>
    		<package name="com.qyxy.mapper"/>//所在包
    		
    	</mappers>
    </configuration>
    

     4.编写类的mapper.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="com.qyxy.mapper.DeptMapper">
    
    	<select id="selectDept" resultType="Dept" databaseId="oracle">
    		select *
    		from dept where deptno = #{deptno}
    	</select>
    	<select id="findAll" resultType="Dept">
    		select * from dept
    	</select>
    	<!-- 查询指定信息 -->
    	<select id="find" resultType="Dept">
    		select * from dept where
    		dname=#{dname} and loc=#{loc}
    	</select>
    	<!-- 新增指定对象 -->
    	<insert id="saveDept">
    		insert into dept(deptno,dname,loc)
    		values(#{deptno},#{dname},#{loc})
    	</insert>
    	<!-- 删除指定对象 -->
    	<delete id="deleteDept">
    		delete from dept where deptno=#{deptno}
    	</delete>
    	<!-- 修改指定对象 -->
    	<update id="updateDept">
    		update dept
    		set dname=#{dname},loc=#{loc}
    		where
    		deptno=#{deptno}
    	</update>
    	
    	<!-- 新增获取自动生成的主键id oracle中有效 -->
    	<insert id="insertDept">
    		 <selectKey keyProperty="deptno" resultType="int" keyColumn="deptno"
    			order="BEFORE">
    			select SEQ_DEPT_DEPTNO.nextval from dual
    		</selectKey> 
    		insert into dept(deptno,dname,loc) values (#{deptno},#{dname},#{loc})
    	</insert>	
    ------------------------------------------------------------------------------------------------补充
      <!-- 新增多个对象 mysql中操作有效 -->
          <insert id="saveDepts" useGeneratedKeys="true" keyProperty="deptno">
              insert into dept(dname,loc) values
              <foreach item="item" collection="list" separator=",">
                  (#{item.dname},#{item.loc})
              </foreach>
          </insert> </mapper>

     5.写mapper接口和测试方法,这里就不一一写了,大家自己测试吧

  • 相关阅读:
    node.js 基础篇
    node.js 开发环境搭建
    Velocity VelocityEngine 支持多种loader 乱码问题
    tomcat集群及session共享
    上海支付宝终面后等了两周,没能收到offer却来了杭州淘宝的电话面试
    支付宝hr终面,忐忑的等待结果
    mysql 数据范围总结
    rsync同步文件(多台机器同步代码...)
    linux基础命令
    路飞学城项目之前后端交互,跨域问题
  • 原文地址:https://www.cnblogs.com/lbloveab/p/7301438.html
Copyright © 2011-2022 走看看