spring开机和mybatis自动增加id
spring boot and mybatis auto increase id
我使用 spring boot 和 mybatis 向 h2 数据库插入记录,同时,使用自动增加的 id。
首先,我和图书馆:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
然后一个域class:
@Repository
package com.example.demo.domain;
public class Customer {
private int id;
private String name;
// getter setter and 3 constructor...
}
然后是映射器:
package com.example.demo.mapper;
import ...;
import com.example.demo.domain.Customer;
@Service
public interface CustomerMapper {
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert("insert into Customer (name) values (#{name})")
public int insert(Customer c);
@Select("select * from Customer")
public List<Customer> sel();
}
在 class 路径的根目录下还有一个 schema.sql 文件,其中包含 sql:
drop table if exists customer
create table customer (id int,name varchar)
然后spring开机class:
package com.example.demo;
import com.example.demo.domain.Customer;
import com.example.demo.mapper.CustomerMapper;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class Demo2Application implements CommandLineRunner{
public static void main(String[] args) {
SpringApplication.run(Demo2Application.class, args);
}
@Autowired
CustomerMapper customerMapper;
@Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
customerMapper.insert(new Customer("john"));
customerMapper.insert(new Customer("james"));
List<Customer> cus = customerMapper.sel();
System.out.println(cus.get(0).getId());
System.out.println(cus.get(1).getId());
}
}
控制台打印:
0
0
表示我插入了两个人,但是他们的id都是0,不是自增
您可以按照以下方式更改schema.sql:
drop table if exists customer;
create table customer
(
id int auto_increment primary key,
name varchar
);
该解决方案应该有效。
P.S。
域模型不应由以下代码中的 spring 容器管理:
@Repository
package com.example.demo.domain;
public class Customer {
private int id;
private String name;
// getter setter and 3 constructor...
}
只需删除@Repository 注解
您应该按如下方式更改架构:
创建 TABLE 客户 (
id int AUTO_INCREMENT,
名称 varchar(255)
主键(id)
);
我使用 spring boot 和 mybatis 向 h2 数据库插入记录,同时,使用自动增加的 id。 首先,我和图书馆:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
然后一个域class:
@Repository
package com.example.demo.domain;
public class Customer {
private int id;
private String name;
// getter setter and 3 constructor...
}
然后是映射器:
package com.example.demo.mapper;
import ...;
import com.example.demo.domain.Customer;
@Service
public interface CustomerMapper {
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert("insert into Customer (name) values (#{name})")
public int insert(Customer c);
@Select("select * from Customer")
public List<Customer> sel();
}
在 class 路径的根目录下还有一个 schema.sql 文件,其中包含 sql:
drop table if exists customer
create table customer (id int,name varchar)
然后spring开机class:
package com.example.demo;
import com.example.demo.domain.Customer;
import com.example.demo.mapper.CustomerMapper;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class Demo2Application implements CommandLineRunner{
public static void main(String[] args) {
SpringApplication.run(Demo2Application.class, args);
}
@Autowired
CustomerMapper customerMapper;
@Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
customerMapper.insert(new Customer("john"));
customerMapper.insert(new Customer("james"));
List<Customer> cus = customerMapper.sel();
System.out.println(cus.get(0).getId());
System.out.println(cus.get(1).getId());
}
}
控制台打印:
0
0
表示我插入了两个人,但是他们的id都是0,不是自增
您可以按照以下方式更改schema.sql:
drop table if exists customer;
create table customer
(
id int auto_increment primary key,
name varchar
);
该解决方案应该有效。
P.S。 域模型不应由以下代码中的 spring 容器管理:
@Repository
package com.example.demo.domain;
public class Customer {
private int id;
private String name;
// getter setter and 3 constructor...
}
只需删除@Repository 注解
您应该按如下方式更改架构:
创建 TABLE 客户 ( id int AUTO_INCREMENT, 名称 varchar(255) 主键(id) );