四 mybatis获取主键列的值(orcale)
来源:互联网 发布:火星哥 知乎 编辑:程序博客网 时间:2024/06/06 17:04
建一个实体类,类属性为表中的列名 ***
接口映射xml:
1.接口定义
public interface FoodInter {
public abstract void saveFood(Food food);
//获取主键列的值
<insert id="saveFood">
<!-- keyProperty :属性名(区分大小写)-->
<selectKey keyProperty="foodId" order="BEFORE" resultType="int" statementType="STATEMENT">
select foods_scr.nextval from dual
</selectKey>
insert into foods values(#{foodId},#{foodName},#{price})
</insert>
3.测试方法:
private SqlSession getSession() throws IOException {
// mybatis核心配置文件路径
String resource = "cn/et/lesson2/xml/mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
// session操作的是指向sql语句的一个唯一标识符
SqlSession openSession = sqlSessionFactory.openSession();
return openSession;
}
@Test
public void saveFood() throws IOException {
SqlSession openSession = getSession();
FoodInter mapper = openSession.getMapper(FoodInter.class);
Food food=new Food();
food.setFoodName("kkk");
food.setPrice("55");
mapper.saveFood(food);
openSession.commit();
System.out.println(food.getFoodId());
}
public interface FoodInter {
@SelectKey(before=true,keyProperty="foodId",statement="select foods_scr.nextval from dual", resultType = int.class)
@Insert("insert into foods values(#{foodId},#{foodName},#{price})")
public abstract void saveFood(Food food);
}
2.测试方法:
private SqlSession getSession() throws IOException {
// mybatis核心配置文件路径
String resource = "cn/et/lesson2/xml/mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
// session操作的是指向sql语句的一个唯一标识符
SqlSession openSession = sqlSessionFactory.openSession();
return openSession;
}
@Test
public void saveFood() throws IOException {
SqlSession openSession = getSession();
FoodInter mapper = openSession.getMapper(FoodInter.class);
Food food=new Food();
food.setFoodName("kkk");
food.setPrice("55");
mapper.saveFood(food);
openSession.commit();
System.out.println(food.getFoodId());
}
接口映射xml:
1.接口定义
public interface FoodInter {
public abstract void saveFood(Food food);
}
2.Mapper.xml://获取主键列的值
<insert id="saveFood">
<!-- keyProperty :属性名(区分大小写)-->
<selectKey keyProperty="foodId" order="BEFORE" resultType="int" statementType="STATEMENT">
select foods_scr.nextval from dual
</selectKey>
insert into foods values(#{foodId},#{foodName},#{price})
</insert>
3.测试方法:
private SqlSession getSession() throws IOException {
// mybatis核心配置文件路径
String resource = "cn/et/lesson2/xml/mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
// session操作的是指向sql语句的一个唯一标识符
SqlSession openSession = sqlSessionFactory.openSession();
return openSession;
}
@Test
public void saveFood() throws IOException {
SqlSession openSession = getSession();
FoodInter mapper = openSession.getMapper(FoodInter.class);
Food food=new Food();
food.setFoodName("kkk");
food.setPrice("55");
mapper.saveFood(food);
openSession.commit();
System.out.println(food.getFoodId());
}
接口映射注解:
1.接口定义:public interface FoodInter {
@SelectKey(before=true,keyProperty="foodId",statement="select foods_scr.nextval from dual", resultType = int.class)
@Insert("insert into foods values(#{foodId},#{foodName},#{price})")
public abstract void saveFood(Food food);
}
2.测试方法:
private SqlSession getSession() throws IOException {
// mybatis核心配置文件路径
String resource = "cn/et/lesson2/xml/mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
// session操作的是指向sql语句的一个唯一标识符
SqlSession openSession = sqlSessionFactory.openSession();
return openSession;
}
@Test
public void saveFood() throws IOException {
SqlSession openSession = getSession();
FoodInter mapper = openSession.getMapper(FoodInter.class);
Food food=new Food();
food.setFoodName("kkk");
food.setPrice("55");
mapper.saveFood(food);
openSession.commit();
System.out.println(food.getFoodId());
}
阅读全文
0 0
- 四 mybatis获取主键列的值(orcale)
- mybatis获取自增主键的值
- 出现ORA-01461:仅能绑定要插入LONG列的LONG值 mybatis+orcale
- Mybatis中的insert获取自增主键的值
- MyBatis基础篇之如何获取Oracle主键的值
- 12、mybatis获取自增主键的值
- 获取模版列控件的主键
- mybatis 向数据库插入数据时获取其主键值(序列值)的实现小记
- mybatis oracle获取插入的主键
- MyBatis 获取insert操作返回的主键
- mybatis获取插入记录的主键
- mybatis oracle获取插入的主键
- Mybatis获取自增主键值
- MyBatis之自增长主键值获取
- mybatis insert获取主键
- mybatis selectkey获取主键
- 获取控件的数据集 在设置主键 在获取该行某列的值
- sql 获取主键列名字
- android 字体上添加横线,字体下面添加下划线
- 快速排序算法
- redis实现session共享
- 关于雪铁龙大灯改装问题探讨
- opencv读取视频失败
- 四 mybatis获取主键列的值(orcale)
- C# 中 "",null,string.Empty 三者的区别
- 【Scikit-Learn 中文文档】14 多类和多标签算法
- Java多线程系列--【基础篇04】-synchronized关键字
- ajks_01
- 关于软件开发技巧
- Java与CSP数据兼容之二:Java兼容CSP导出的RSA私钥数据
- soap语法
- ACM/CCPC国内赛题型