Spring整合Mybatis入门(注解版)
步骤一:
创建maven项目在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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring_test03</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--MySQL驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
步骤二:
在resources资源文件夹下创建jdbc.properties配置文件用来输入数据库中的信息
jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmdb?useSSL=false
jdbc.username=root
jdbc.password=123456
在config包下创建配置类SpringConfig,并且创建MybatisConfig与JdbcConfig配置类分别来配置连接数据库的信息以及mybatis的映射关系。
在SpringConfig配置类中通过@Import注解来导入JdbcConfig与MybatisConfig配置类,通过@PropertySource注解来引入资源文件夹下的jdbc.properties文件信息
导入式相比于扫描式的优点:不会导致配置类的臃肿,且在配置类寻找这些功能的时候不需要在一一寻找对应的关联bean。
ps:使用@Import注解手动加入到配置类到核心配置类中,此注解只能够使用一次,在需要多个配置类时需要使用数组的格式导入配置。
SpringConfig配置类
package com.zhicheng.lq.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan("com.zhicheng.lq")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {
}
JdbcConfig配置类
由于已经通过导入式的方法让SpringConfig配置类加载到了JdbcConfig配置类,SpringConfig也引入了资源文件夹下的jdbc.properties文件,所以JdbcConfig配置类中可以使用@Value注解使其赋值参数信息。
package com.zhicheng.lq.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
MybatisConfig配置类
package com.zhicheng.lq.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class MybatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setTypeAliasesPackage("com.zhicheng.lq.entiy");
return factoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.zhicheng.lq.dao");
return msc;
}
}
步骤三:
- 在entiy包中创建User实体类用来存放数据库获取的信息
- 完成dao层与serevice层的代码编写
entiy包中的User实体类
package com.zhicheng.lq.entiy;
public class User {
private Integer id;
private String name;
private Integer age;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public User(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public User() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
dao包中的UserDao接口类
package com.zhicheng.lq.dao;
import com.zhicheng.lq.entiy.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserDao {
@Select("select * from tbl_user")
List<User> selectAll();
}
service层中的UserService接口类
package com.zhicheng.lq.service;
import com.zhicheng.lq.entiy.User;
import java.util.List;
public interface UserService {
List<User> getAllUser();
}
serevice层中的UserServiceImp实现类
package com.zhicheng.lq.service.Imp;
import com.zhicheng.lq.dao.UserDao;
import com.zhicheng.lq.entiy.User;
import com.zhicheng.lq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImp implements UserService {
@Autowired
private UserDao userDao;
public List<User> getAllUser() {
return userDao.selectAll();
}
}
步骤四:
编写测试类Test用于测试结果
Tset测试类
package com.zhicheng.lq;
import com.zhicheng.lq.config.SpringConfig;
import com.zhicheng.lq.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Tset {
public static void main(String[] args) {
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService=ac.getBean(UserService.class);
System.out.println(userService.getAllUser());
}
}
结果展示:
项目结构展示:
标签:Spring,org,lq,zhicheng,注解,Mybatis,import,com,public From: https://www.cnblogs.com/HappyLinq/p/16910368.html