注解配置MySQL数据库表uuid主键

@Id
@GenericGenerator(name="systemUUID",strategy="uuid")
@GeneratedValue(generator="systemUUID")
@Column(name = "id", nullable = false)
private String id;
在Spring Boot中整合MyBatis并使用MySQL实现主键UUID的方法如下: 1. 首先,在MySQL数据库中创建一个,其中包含一个varchar类型的字段用于存储UUID。示例结构如下: ``` CREATE TABLE `example_table` ( `id` varchar(36) NOT NULL, `name` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ``` 2. 在Spring Boot的配置文件(application.properties或application.yml)中,配置数据库连接信息。 ``` spring.datasource.url=jdbc:mysql://localhost:3306/example_database spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3. 创建一个实体类Example,数据库中的。 ```java public class Example { private String id; private String name; // getter和setter方法省略... } ``` 4. 创建一个Mapper接口ExampleMapper,用于执行数据库操作。 ```java public interface ExampleMapper { void insertExample(Example example); Example selectExampleById(String id); // 其他数据库操作方法... } ``` 5. 创建一个Mapper的XML文件ExampleMapper.xml,定义对应的SQL语句。 ```xml <mapper namespace="com.example.mapper.ExampleMapper"> <insert id="insertExample" parameterType="com.example.entity.Example"> INSERT INTO example_table(id, name) VALUES (#{id}, #{name}) </insert> <select id="selectExampleById" parameterType="java.lang.String" resultType="com.example.entity.Example"> SELECT * FROM example_table WHERE id = #{id} </select> <!-- 其他SQL语句定义... --> </mapper> ``` 6. 在Spring Boot的主类中使用@MapperScan注解扫描Mapper接口所在的包。 ```java @SpringBootApplication @MapperScan("com.example.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 7. 然后就可以在代码中使用ExampleMapper接口进行数据库操作了。在需要插入数据的地方,使用UUID生成一个唯一的ID,并将其设置到实体类中,然后调用insertExample方法插入数据。 ```java @Autowired private ExampleMapper exampleMapper; public void insertExample() { Example example = new Example(); example.setId(UUID.randomUUID().toString()); example.setName("example name"); exampleMapper.insertExample(example); } ``` 通过以上步骤,就可以在Spring Boot项目中通过MyBatis和MySQL实现主键UUID的方式进行数据操作了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值