记录一下用springboot3整合mybatis,测试几个简单的功能

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.1.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

  <!-- Generated by https://start.springboot.io -->
  <!-- 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn -->
	<groupId>com.example</groupId>
	<artifactId>SpringBootMyBatisDemo02</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBootMyBatisDemo02</name>
	<description>SpringBootMyBatisDemo02</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>3.0.2</version>
		</dependency>

		<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter-test</artifactId>
			<version>3.0.2</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

更改application为yml

做数据源配置:

# 数据源配置:
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: root

在resources下新建目录mapper

继续在yml内做mybatis配置

        

#mybatis配置:
mybatis:
  mapper-locations: classpath:mapper/*.xml #加入mapper.xml的扫描
  type-aliases-package: com.example.entity #设置实体类别名

#开启mybatis对应的sql的日志
logging:
  level:
    com.example.mapper: trace

 数据库user表

创建实体类User

@NoArgsConstructor
@AllArgsConstructor
@Data
public class User {

    private Integer id;
    private String name;
    private String pwd;
}

创建UserMapper 简单增删改查一下


public interface UserMapper {
    //查询 : 根据主键查询一条用户数据
    User selectOneUser(Integer id);
    //保存
    int save(User user);
    //更新
    int update(User user);
    //删除 : 根据主键id删除一条用户数据
    int delete(Integer id);

}

在resource下的mapper目录中创建UserMapper.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="com.example.mapper.UserMapper">
    <select id="selectOneUser" parameterType="int" resultType="User">
        select * from user where id = #{arg0}
    </select>

    <insert id="save" parameterType="User">
        insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
    </insert>

    <update id="update" parameterType="User">
        update user set name = #{name},pwd = #{pwd} where id = #{id}
    </update>

    <delete id="delete" parameterType="int">
        delete from user where id = #{arg0}
    </delete>
</mapper>

创建接口UserService

public interface UserService {

    User findOneUser(Integer id);
    int save(User user);
    int update(User user);
    int deleteUserById(Integer id);
}

创建实现类UserServiceImpl 这里会爆红因为还没有写Controller

 

@Service
public class UserServiceImpl implements UserService {

    // 连接mapper层 用Autowired注解注入进来
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findOneUser(Integer id) {
        return userMapper.selectOneUser(id);
    }

    @Override
    public int save(User user) {
        return userMapper.save(user);
    }

    @Override
    public int update(User user) {
        return userMapper.update(user);
    }

    @Override
    public int deleteUserById(Integer id) {
        return userMapper.delete(id);
    }
}

创建UserController

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/user/{id}")
    @ResponseBody
    public User select(@PathVariable Integer id){
        return userService.findOneUser(id);
    }

    @PostMapping("/user")
    @ResponseBody
    public int save(@RequestBody User user){
        return userService.save(user);
    }

    @PutMapping("/user")
    @ResponseBody
    public int update(@RequestBody User user){
        return userService.update(user);
    }

    @DeleteMapping("/user/{id}")
    @ResponseBody
    public int delete(@PathVariable Integer id){
        return userService.deleteUserById(id);
    }
}

测试

启动后报错

说对应的UserMapper找不到,漏加了包扫描

补上@MapperScan

@SpringBootApplication
@MapperScan("com.example.mapper")
public class SpringBootMyBatisDemo02Application {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootMyBatisDemo02Application.class, args);
	}

}

启动成功

使用apipost开始测试

刷新数据库user表

刷新user表

刷新user表

这样所有的功能都没问题了

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C#Java 都支持 AES 加密算法,因此可以在两种语言中进行加密和解密。下面是一个示例代码,演示了 C#Java 中如何使用 AES 加密和解密数据。 首先是 Java 中的代码,用于加密数据: ```java import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class AesEncryption { private static final String ALGORITHM = "AES/CBC/PKCS5Padding"; private static final String KEY = "0123456789abcdef"; // 16-byte key private static final String IV = "0123456789abcdef"; // 16-byte initialization vector public static String encrypt(String data) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHM); SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), "AES"); IvParameterSpec ivSpec = new IvParameterSpec(IV.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); byte[] encrypted = cipher.doFinal(data.getBytes()); return Base64.getEncoder().encodeToString(encrypted); } } ``` 这个代码使用了 AES/CBC/PKCS5Padding 加密算法,采用了 16 字节的密钥和初始化向量。`encrypt()` 方法接受一个字符串参数,并返回加密后的字符串。 接下来是 C# 中的代码,用于解密数据: ```csharp using System; using System.Security.Cryptography; using System.Text; public class AesDecryption { private static readonly byte[] Key = Encoding.UTF8.GetBytes("0123456789abcdef"); // 16-byte key private static readonly byte[] Iv = Encoding.UTF8.GetBytes("0123456789abcdef"); // 16-byte initialization vector public static string Decrypt(string data) { byte[] encryptedData = Convert.FromBase64String(data); using (Aes aes = Aes.Create()) { aes.Key = Key; aes.IV = Iv; aes.Padding = PaddingMode.PKCS7; aes.Mode = CipherMode.CBC; ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); byte[] decrypted = decryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length); return Encoding.UTF8.GetString(decrypted); } } } ``` 这个代码使用了相同的 AES/CBC/PKCS5Padding 加密算法和 16 字节的密钥和初始化向量。`Decrypt()` 方法接受一个加密的字符串参数,并返回解密后的字符串。 使用这两个类,可以在 C#Java 中进行 AES 加密和解密操作。注意,密钥和初始化向量需要在两种语言中保持一致。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值