简单说明,使用idea连接mysql数据库的方法有很多种,目前来说学习了以下三种方法,个人建议是使用第二种方法,便于理解,不过企业级开发很多还是第三种方法,因为第三种方法使用xml配置文件,便于管理和数据源的多级分配,比较适合大型项目,也是一种传统。不过听过基于注解开发才是未来的趋势,所以还是推荐使用注解开发(第二种方法)
简要思路:
方法一
1、导入pom.xml
2、编写实体类(user类)和controller类
3、编写接口类(userMapper类),编写UserMapper.xml(映射)文件
4、编写application.properties文件
5、启动程序
方法二
1、导入pom.xml
2、编写实体类(user类)和controller类(此类也可以最后写,为了书写就放在这了)
3、编写接口类(userMapper类),sql语句直接用注解形式写在方法上
4、编写application.yml文件设置好数据源
5、启动程序
方法三
1、导入pom.xml
2、编写实体类(user类)、接口类(userMapper类)和controller类
3、编写Mapper.xml文件
4、编写application.properties文件设置好数据源
5、编写mybatisConfig.xml导入数据源,导入编写好的Mapper.xml文件
6、编写userConfig类,启动程序
方法一
**使用application.properties文件配置数据源,使用web访问controller层,controller层调用mapper层(也就是dao层,不同的说法而已,不用在意)然后mapper中的sql查询语句写到xml文件中。**
1、先导入pom.xml依赖
<!--导入mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2、创建一个user实体类(注解真的好用)
package com.huahua.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author zzuli_huahua
* @version 1.0
* @date 2021/6/3 21:07
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String username;
private String password;
}
2、创建一个UserMapper类
package com.huahua.mapper;
import com.huahua.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author zzuli_huahua
* @version 1.0
* @date 2021/6/3 21:38
*/
@Mapper
@Repository
public interface UserMapper {
List<User> getAllUser();
List<User> queryUserList(); //本实例中 实际用到的只有这个
User queryUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
3、编写UserMapper.xml文件(用来存放sql语句的文件)
<?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.huahua.mapper.UserMapper">
<select id="queryUserList" resultType="com.huahua.pojo.User">
select * from User
</select>
</mapper>
4、配置application.properties文件
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#整合mybatis
mybatis.type-aliases-package= com.huahua.pojo
mybatis.mapper-locations= classpath:mybatis/mapper/*.xml
不是很懂可以看一下图片解释
5、创建一个UserControllerr类
package com.huahua.controller;
import com.huahua.mapper.UserMapper;
import com.huahua.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author zzuli_huahua
* @version 1.0
* @date 2021/6/3 22:10
*/
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/queryUserList")
public List<User> queryUserList(){
List<User> userList = userMapper.queryUserList();
return userList;
}
}
6、运行程序、用web访问
访问地址:http://localhost:8080/queryUserList
1、查询出来的结果和数据库一样,连接数据库成功。
2、其实在写sql语句的时候,现在是写在xml文件中的,另一种方法就是可以用注解的方式写在UserMapper类上;
3、还有一些问题,就是连接数据库的数据源应该独立写在一个properties文件里面,这样容易维护,如果要修改数据库的地址的时候直接修改稿db.properties文件就可以了
以上就是连接数据库的第一种方法,说实话,这种连接数据库的方法有些不妥,因为耦合性有点高,不符合springboot的思想。
下面是另一种连接数据库的方法
方法二(使用注解开发,简单、便捷)
1、首先导入pom.xml依赖
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、在resources目录下创建一个application.yml文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/test
3、创建一个User类
package com.huahua.pojo;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author zzuli_huahua
* @version 1.0
* @date 2021/6/3 21:07
*/
public class User {
private int id;
private String username;
private String password;
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;
}
}
4、创建一个UserMapper接口类
package com.huahua.mapper;
import com.huahua.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author zzuli_huahua
* @version 1.0
* @date 2021/6/3 21:38
*/
@Mapper
@Repository
public interface UserMapper {
@Select("SELECT * FROM user") //注解开发,查询User全部信息
List<User> findAllUser();
}
5、创建一个UserController类
package com.huahua.controller;
import com.huahua.mapper.UserMapper;
import com.huahua.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author zzuli_huahua
* @version 1.0
* @date 2021/6/4 16:51
*/
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/findAll")
public List<User> findAll(){
List<User> allUser = userMapper.findAllUser();
return allUser;
}
}
6、打开浏览器访问 http://localhost:8080/findAll
与数据库的user表比较
结果显示查询成功
第三种方法(算是常规吧)
1、导入依赖(完整的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>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.huahua</groupId>
<artifactId>springboot-mybatis2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatis2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<!--导入mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</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>junit</groupId>
<artifactId>junit</artifactId>
</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>
2、编写User类
package com.huahua.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author zzuli_huahua
* @version 1.0
* @date 2021/6/3 21:07
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String username;
private String password;
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;
}
}
3、编写UserMapper类
package com.huahua.mapper;
import com.huahua.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author zzuli_huahua
* @version 1.0
* @date 2021/6/3 21:38
*/
@Mapper
@Repository
public interface UserMapper {
List<User> queryUserList();
}
4、编写UserContrlloer类
package com.huahua.controller;
import com.huahua.mapper.UserMapper;
import com.huahua.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author zzuli_huahua
* @version 1.0
* @date 2021/6/3 22:10
*/
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/queryUserList")
public List<User> queryUserList(){
List<User> userList = userMapper.queryUserList();
return userList;
}
}
5、编写UserMapper.xml文件(用来存放sql语句的文件)
<?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.huahua.mapper.UserMapper">
<select id="queryUserList" resultType="com.huahua.pojo.User">
select * from User
</select>
</mapper>
6、在application.properties文件内编写数据源
spring.datasource.username= root
spring.datasource.password= root
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
spring.datasource.url= jdbc:mysql://localhost:3306/test
#mybatis.mapper-locations来指明mapper的xml文件存放位置
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
#mybatis.type-aliases-package来指明和数据库映射的实体的所在包。
mybatis.type-aliases-package=com/huahua/pojo
7、编写mybatisConfig.xml配置数据源和导入xml文件
<?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>
<!-- 导入配置文件信息 resource是根目录下的文件路径-->
<properties resource="application.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--这个是写死的数据源,不建议用,但可以用 -->
<!-- <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>-->
<!-- 从配置文件中加载属性 -->
<property name="driver" value="${spring.datasource.driver-class-name}"/>
<property name="url" value="${spring.datasource.url}"/>
<property name="username" value="${spring.datasource.username}"/>
<property name="password" value="${spring.datasource.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 加载前面编写的SQL语句的文件 -->
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
8、编写Config类用来扫描xml文件(可以当作启动连接数据库的类)
package com.huahua.config;
import com.huahua.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.io.InputStream;
/**
* @author zzuli_huahua
* @version 1.0
* @date 2021/6/4 21:11
*/
@Configuration
public class userConfig {
private UserMapper userMapper;
@Before
@Bean
public void before() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");//扫描数据源的xml文件
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
userMapper = sqlSession.getMapper(UserMapper.class);
}
}
9、运行效果
点击运行,然后用浏览器访问http://localhost:8080/queryUserList
结果如下图所示
温馨提示:一定要启动整个项目而不是test,正确执行如图所示
感谢您的观看,由于时间仓促和本人水平有限,难免出现纰漏之处,还请各位大佬多多指正,留下宝贵的评论与建议!再次感谢各位的观看!