spring整合mabatis框架,duird连接池,Junit5测试框架
1)创建Maven工程
2)导入相关的依赖
<!--springContext依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.1.12</version> </dependency> <!--mybatis依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.16</version> </dependency> <!--druid连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.7</version> </dependency> <!--JUNIT5单元测试--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>6.1.12</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.11.0</version> <scope>test</scope> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.22</version> </dependency> <!--mybatis-Spring--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>3.0.4</version> </dependency> <!--spirng-tx--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.3.22</version> </dependency>
3)导入sql
1.新建数据库mabits
注:我这里使用的工具是navicat
2.导入mysql
1-创建user表
/*
Navicat Premium Data Transfer
Source Server : mybatis
Source Server Type : MySQL
Source Server Version : 80025
Source Host : localhost:3306
Source Schema : mybatis
Target Server Type : MySQL
Target Server Version : 80025
File Encoding : 65001
Date: 13/09/2024 22:45:23
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '用户id',
`username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户名字',
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户密码',
`userInfo` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户信息',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
2-下载sql
sql文件下载地址: user.sql官方版下载丨最新版下载丨绿色版下载丨APP下载-123云盘
3-新建数据库
4-找到sql文件
在新的见面点击三个点,然后找到你下载的sql位置,打开
5-执行sql
点击start执行sql
sql执行成功
6-查看界面
在新的界面中我们可以看到
4)整合mybatis
1.创建User实体
注:User实体的属性名与数据库中表的列名相对应
User类
package org.xiji.enty;
public class User {
private int id;
private String username;
private String password;
private String userInfo;
public User() {
}
public User(int id, String username, String password, String userInfo) {
this.id = id;
this.username = username;
this.password = password;
this.userInfo = userInfo;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserInfo() {
return userInfo;
}
public void setUserInfo(String userInfo) {
this.userInfo = userInfo;
}
}
创建enty包==创建User类
2.创建UserMapper类
UserMapper类
package org.xiji.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.xiji.enty.User;
/**
* userMapper
*/
@Mapper
public interface UserMapper {
/**
* 增加用户
*/
int addUser(@Param("user") User user);
}
创建mapper包===》创建UserMapper(为interface类型)
3.创建UserMapper.xml文件
在resources文件中创建UserMapper.xml文件
注:mapper.xml的命名空间为mapper接口(interface)的包名+类名
4.创建jdbc.properties文件
在resource目录下创建jdbc.properties.
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=root
5.创建mybatisConfing.xml文件
在resources文件夹下创建mybatisConfing.xml文件
注:这里面是mybatis的一些全局设置信息
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="multipleResultSetsEnabled" value="true"/> </settings> </configuration>
6.创建springConfig.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--导入jdbc.properties--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置druid连接池--> <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--配置Sql连接工程的数据源和mybatis配置文件--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatisConfig.xml"/> <!--指定mapper xml位置--> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!--配置mapper扫描--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="org.xiji.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!--开启组件扫描--> <context:component-scan base-package="org.xiji"/> </beans>
图解
7.创建MabatisTest类
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.xiji.enty.User;
import org.xiji.mapper.UserMapper;@SpringJUnitConfig(locations = {"classpath:springConfig.xml"})
public class MyBatisTest {@Autowired
UserMapper userMapper;@Test
public void test(){
User user = new User();
user.setUsername("xiji");
user.setPassword("123456");
user.setUserInfo("hello world");
userMapper.addUser(user);
}
}
8.测试
9.附:数据库密码错误
错误收集
9月 14, 2024 12:47:22 上午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
9月 14, 2024 12:47:22 上午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl error
严重: create connection SQLException, url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8, errorCode 1045, state 28000
java.sql.SQLException: Access denied for user 'root '@'localhost' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:130)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:446)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:239)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:188)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1657)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1723)
at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2838)
原因:密码后面多一个空格
解决方案
把空格去掉就可以了