文章目录
1.导入整合需要的jar包
<!-- Spring操作数据库包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
<!-- Spring整合Mybatis包,Mybatis提供-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
2. properties 资源包数据
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db?useServerPrepStmts=true
jdbc.username=root
jdbc.password=root
3.数据源配置类
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 ds=new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
}
4. Mybatis 配置类(替换 Mybatis 的 xml 配置),并配置 SqlSeesionFactoryBean(主要)
public class MybatisConfig {
//定义bean,SqlSessionFactoryBean,用于创建SqlSessionFactory对象
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean ssfb=new SqlSessionFactoryBean();
//设置模型类的别名
ssfb.setTypeAliasesPackage("com.my.domain");
//设置数据源
ssfb.setDataSource(dataSource);
return ssfb;
}
//返回 MapperScannerConfigurer 对象
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc=new MapperScannerConfigurer();
//包扫描
msc.setBasePackage("com.my.dao");
return msc;
}
}
5.Spring配置类(导入所有外部配置类,资源文件)
@Configuration
@ComponentScan("com.my")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringCong {
}
6.测试
配置类以及资源包同上
数据库
- 实体类
public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
- 数据层接口(注解方式)
public interface UserDao {
@Insert("insert into user_tb(username, password) VALUES (#{username},#{password})")
void save(User user);
@Delete("delete from user_tb where id=#{id}")
void delete(Integer id);
@Update("update user_tb set username=#{username},password=#{password}")
void update(User user);
@Select("select * from user_tb")
List<User> selectAll();
}
- 业务层实现类(业务层接口略)
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public void save(User user) {
userDao.save(user);
}
@Override
public void delete(Integer id) {
userDao.delete(id);
}
@Override
public void update(User user) {
userDao.update(user);
}
@Override
public List<User> selectAll() {
return userDao.selectAll();
}
}
- 测试类
public class MyApp {
public static void main(String[] args) {
ApplicationContext applicationContext=new AnnotationConfigApplicationContext(SpringCong.class);
UserService userService = (UserService) applicationContext.getBean("userService");
List<User> users = userService.selectAll();
System.out.println(users);
//输出结果为:
// [User{id=1, username='张嘉圣杰', password='123456'}, User{id=2, username='张飘飘', password='456789'}]
}
}