springboot+mybatis项目
2023-12-30 20:32:54
1:pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>item-center</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>item-center</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>org.example.AppMain</start-class>
<java.version>1.8</java.version>
<lombok.version>1.14.8</lombok.version>
<log4jdbc.log4j2.version>1.16</log4jdbc.log4j2.version>
<rest.assured.version>2.3.3</rest.assured.version>
</properties>
<dependencies>
<!--SpringBoot启动依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!--集成mysql数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.6.1</version>
</dependency>
<!--spring boot集成mybatis的依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.23</version>
</dependency>
</dependencies>
</project>
2:yml配置
spring:
profiles:
active: dev
spring:
application:
name: item-center
datasource:
url: jdbc:mysql://127.0.0.1:3306/item-center
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
mapper-locations: classpath:mybatis-mapper/*.xml
3:sql
CREATE TABLE `item-center`.`t_item` (
`id` bigint(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'id',
`name` varchar(50) NULL COMMENT '名称',
`price` bigint(11) NULL COMMENT '价格',
PRIMARY KEY (`id`)
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COMMENT = '商品';
4:entity
package org.example.entity;
import java.io.Serializable;
public class ItemDO implements Serializable {
private long id;
private String name;
private long price;
}
5:mapper.xml
<?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="org.example.mapper.ItemMapper">
<select id="listAll" resultType="org.example.entity.ItemDO">
select id,name,price from t_item
</select>
<select id="get" resultType="org.example.entity.ItemDO">
select id,name,price from t_item where id=#{id}
</select>
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into t_item(name,price)
values(#{name},#{price})
</insert>
<update id="update">
update t_item set
name=#{name},
price=#{price}
where id=#{id}
</update>
</mapper>
6:mapper.class
package org.example.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.example.entity.ItemDO;
import java.util.List;
@Mapper
public interface ItemMapper {
public int insert(ItemDO itemDO);
public int update(ItemDO itemDO);
public int get(long id);
public List<ItemDO> listAll();
}
7:test
package org.example;
import com.alibaba.fastjson.JSON;
import org.example.entity.ItemDO;
import org.example.mapper.ItemMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Random;
import java.util.UUID;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {AppMain.class})
public class ItemTest {
@Autowired
private ItemMapper itemMapper;
@Test
public void itemAllTest(){
ItemDO itemDO = new ItemDO();
itemDO.setName("名称"+UUID.randomUUID().toString());
itemDO.setPrice(new Random().nextInt(1000));
itemMapper.insert(itemDO);
System.out.println(JSON.toJSON(itemDO));
itemDO.setName("改过后的——>"+itemDO.getName());
itemMapper.update(itemDO);
List list = itemMapper.listAll();
System.out.println(JSON.toJSONString(list));
}
}
文章来源:https://blog.csdn.net/qq_36577291/article/details/135308663
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!