🐌个人主页: 🐌 叶落闲庭
💨我的专栏:💨
c语言
数据结构
javaweb
石可破也,而不可夺坚;丹可磨也,而不可夺赤。
Spring整合
- 一、Spring整合Mybatis
- 1.1 整合Mybatis,原配置文件
- 1.2整合后
- 1.3相关文件:
- 1.3.1jdbc配置文件
- 1.3.2jdbc配置类及spring配置类
- 1.3.3数据库操作类
- 1.3.4测试类
- 1.3.5运行结果
- 二、Spring整合JUnit
- 2.1使用Spring整合JUnit专用的类加载器
- 总结
一、Spring整合Mybatis
1.1 整合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>
<properties resource="jdbc.properties"></properties>
<typeAliases>
<package name="com.practice.domain"/>
</typeAliases>
<environments default="development">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com.practice.dao"/>
</mappers>
</configuration>
1.2整合后
package com.practice.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
/**
* @Author YJ
* @Date 2023/7/31 10:10
* Description:Mybatis配置
*/
public class MybatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
ssfb.setTypeAliasesPackage("com.practice.domain");
ssfb.setDataSource(dataSource);
return ssfb;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.practice.dao");
return msc;
}
}
1.3相关文件:
1.3.1jdbc配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?useSSL=false
jdbc.userName=root
jdbc.password=123456
1.3.2jdbc配置类及spring配置类
package com.practice.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
/**
* @Author YJ
* @Date 2023/7/31 10:00
* Description:Jdbc配置
*/
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;
}
}
package com.practice.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;
/**
* @Author YJ
* @Date 2023/7/31 9:52
* Description:
*/
@Configuration
@ComponentScan("com.practice")
@PropertySource("jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {
}
1.3.3数据库操作类
public interface UserDao {
void save(User user);
@Insert("insert into tb_user(id,username,password,gender,addr) values(#{id},#{username},#{password},#{gender},#{addr})")
void insert(User user);
@Delete("delete from tb_user where id=#{id}")
void delete(Integer id);
@Update("update tb_user set username=#{username} where id=#{id}")
void update(User user);
@Select("select * from tb_user")
List<User> selectAll();
@Select("select * from tb_user where id=#{id}")
User selectById(Integer id);
}
public interface UserService {
void save(User user);
void update(User user);
void delete(Integer id);
User selectById(Integer id);
List<User> selectAll();
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
public void save(User user) {
userDao.save(user);
}
public void update(User user) {
userDao.update(user);
}
public void delete(Integer id) {
userDao.delete(id);
}
public User selectById(Integer id) {
return userDao.selectById(id);
}
public List<User> selectAll() {
return userDao.selectAll();
}
}
1.3.4测试类
public class App2 {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService bean = ctx.getBean(UserService.class);
User user = bean.selectById(2);
System.out.println(user);
}
}
1.3.5运行结果
二、Spring整合JUnit
2.1使用Spring整合JUnit专用的类加载器
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testSelectById() {
System.out.println(userService.selectById(2));
}
@Test
public void testSelectAll(){
System.out.println(userService.selectAll());
}
}
总结
关于Spring整合的相关步骤就介绍完了,欢迎各位点赞+关注!!!