1、对于支持自增主键的数据库,mybatis直接从数据库获取自增主键值,
作用:在进行添加操作后获取主键值
2、mapper配置文件中insert标签的属性:
①parameterType:可不写
②useGeneratedKeys:使用自增主键获取主键值策略
③keyProperty:指定对应主键属性名(即获取主键值之后,赋值给bean的哪个属性)
3、对于不支持自增主键的数据库,mybatis是从数据库的序列中获取主键值的,如:Oracle中的SEQ
4、mapper配置文件中insert标签的属性:
①keyProperty:指定查出主键封装到哪个Bean的哪个属性
②order:设置是在插入sql前还是后运行获取自增主键的sql(默认值:BEFORE/AFTER)
③resultType:查出数据的返回类型
插入语句的#{id}就是当order="BEFORE"时,selectKey获取到的id的值
5、BEFORE运行顺序:
①运行selectKey查询id的sql,查出id封装给Bean的对应属性
②运行插入sql,完成后可在java取出id属性的对应值
AFTER运行顺序:
①运行插入sql,从序列中取出新值作为id
②运行selectKey查询id的sql
当order=“BEFORE”时的写法:
select xxx_SEQ.nextval from dual
insert into info (id,name,password,description) values (#{id},#{name},#{password},#{description})
当order=“AFTER”时的写法:
select xxx_SEQ.currval from dual
insert into info (id,name,password,description) values (xxx_SEQ.nextval,#{name},#{password},#{description})
6、mybatis.xml
7、InfoMapper.xml
insert into info (name,password,description) values (#{name},#{password},#{description})
8、dbConfig.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mybatis
jdbc.username=root
jdbc.password=
9、Info.java
package com.demo.ssmtest;
public class Info {
Integer id;
String name;
String password;
String description;
public Info() {
super();
}
public Info(Integer id, String name, String password, String description) {
super();
this.id = id;
this.name = name;
this.password = password;
this.description = description;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Info [id=" + id + ", name=" + name + ", password=" + password + ", description=" + description + "]";
}
}
10、InfoMapper.java
package com.demo.ssmtest;
public interface InfoMapper {
public Integer add(Info info);
}
11、TestMain.java
package com.demo.ssmtest;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class TestMain {
public static void main(String[] args) throws Exception {
test();
}
public static void test() throws Exception {
String resource = "mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession openSession = sqlSessionFactory.openSession();
try {
//增
InfoMapper addMapper = openSession.getMapper(InfoMapper.class);
Info add_Info = new Info(null,"nameNew","qweqwe","dddddddd");
int add_Index = addMapper.add(add_Info);
System.out.println("---"+add_Info.getId());
System.out.println(add_Index);
} finally {
openSession.close();
}
}
}
13、demo
https://download.csdn.net/download/qq_22778717/10714876