mybatis使用实体类,在插入的时候,可以返回刚插入的数据,这个功能比较灵活。
1、oracle数据连接
jdbc.properties
driver=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@localhost:1521:XE username=hr password=hr #url=jdbc:oracle:thin:@localhost:1521:XE
2、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> <!-- 读取properties文件 --> <properties resource="jdbc.properties"/> <!--别名 这个放在开头位置,不然可能会报错--> <typeAliases> <typeAlias alias="Jobs" type="com.test.bean.Jobs"/> </typeAliases> <!-- 配置数据源 oracle--> <environments default="development"> <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> </environments> <mappers> <mapper resource="com/test/config/resultMap.xml"/> </mappers> </configuration>
package com.test.bean; public class Jobs { public Jobs() { super(); } private String JOB_ID;//职业Id private String JOB_TITLE;//职业名称 private String MIN_SALARY;//最少薪水 private String MAX_SALARY;//最多薪水 public String getJOB_ID() { return JOB_ID; } public void setJOB_ID(String jOB_ID) { JOB_ID = jOB_ID; } public String getJOB_TITLE() { return JOB_TITLE; } public void setJOB_TITLE(String jOB_TITLE) { JOB_TITLE = jOB_TITLE; } public String getMIN_SALARY() { return MIN_SALARY; } public void setMIN_SALARY(String mIN_SALARY) { MIN_SALARY = mIN_SALARY; } public String getMAX_SALARY() { return MAX_SALARY; } public void setMAX_SALARY(String mAX_SALARY) { MAX_SALARY = mAX_SALARY; } }
<?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.test.bean"> <!-- type别名 id标识 --> <resultMap type="Jobs" id="Jobs_Map"> <!-- id主键 result其他属性 property实体属性 column表对应字段 --> <id property="JOB_ID" column="JOB_ID"/> <result property="JOB_TITLE" column="JOB_TITLE"/> <result property="MIN_SALARY" column="MIN_SALARY"/> <result property="MAX_SALARY" column="MAX_SALARY"/> </resultMap> <!-- 注:使用实体类的方式可以取得主键 mybatis自增主键,并返回 【两种方法】--> <insert id="INSERT_resultMap" parameterType="Jobs" useGeneratedKeys="true" keyProperty="JOB_ID"> <!-- mysql和oracle不同 数据库要设置自增 <selectKey keyProperty="ID" resultType="String" order="BEFORE"> select yoursequence.nextval as "JOB_ID" from dual </selectKey> --> insert into JOBS( JOB_ID, JOB_TITLE, MIN_SALARY, MAX_SALARY) values( #{JOB_ID,jdbcType=VARCHAR}, #{JOB_TITLE,jdbcType=VARCHAR}, #{MIN_SALARY,jdbcType=VARCHAR}, #{MAX_SALARY,jdbcType=VARCHAR}) </insert> </mapper>
public void main(String[] arg){ Jobs jobs = new Jobs(); jobs.setJOB_TITLE("测试"); jobs.setMAX_SALARY("999"); jobs.setMIN_SALARY("1"); SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession(); session.insert("INSERT_resultMap",jobs); session.commit(); session.close(); System.out.println(jobs.getJOB_ID()); System.out.println(jobs.getJOB_TITLE()); System.out.println(jobs.getMAX_SALARY()); System.out.println(jobs.getMIN_SALARY()); }