Spring整合MyBatis快速入门之纯注解
发布日期:2022-02-10 11:36:57 浏览次数:43 分类:技术文章

本文共 4136 字,大约阅读时间需要 13 分钟。

Spring注解整合MyBatis

分析:将Spring核心配置文件所有内容迁移到Spring核心配置类中
整合流程

1、JDBC配置类

package com.project.config;import com.alibaba.druid.pool.DruidDataSource;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.stereotype.Component;import javax.sql.DataSource;@Componentpublic class JdbcConfig1 {
@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 getDataSource(){
DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driver); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; }}

2、MyBatis配置类

package com.project.config;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.mapper.MapperScannerConfigurer;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import javax.sql.DataSource;@Componentpublic class MyBatisConfig1 {
/** *获取SqlSessionFactoryBean */ public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource) {
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); ssfb.setTypeAliasesPackage("com.project.domain"); ssfb.setDataSource(dataSource); return ssfb; } /** *加载mybatis映射配置的扫描,作为spring的bean进行管理 */ public MapperScannerConfigurer getMapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer(); msc.setBasePackage("com.project.dao"); return msc; }}

3、核心配置类,导入上面的配置

package com.project.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")//包扫描@PropertySource("classpath:jdbc.properties")//引入外部配置文件@Import({
JdbcConfig.class,MyBatisConfig.class})//导入其他配置信息public class SpringConfig {
}

资源文件Jdbc.properties

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/spring_dbjdbc.username=rootjdbc.password=admin

4、service的开发

package com.project.service.impl;import com.project.dao.AccountDao;import com.project.domain.Account;import com.project.service.AccountService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class AccountServiceImpl implements AccountService {
@Autowired private AccountDao accountDao; /** * @param id * @return 返回指定id的Account对象 */ public Account findById(Integer id) {
return accountDao.findById(id); }}

5、mapper接口开发,这里使用注解

package com.project.dao;import com.project.domain.Account;import org.apache.ibatis.annotations.Select;public interface AccountDao1 {
/** * @param id * @return 返回指定id的Account对象 */ @Select("select * from account where id = #{id}") Account findById(Integer id);}

6、实体类

package com.project.domain;import java.io.Serializable;public class Account implements Serializable {
private Integer id; private String name; private Double money; /*省略getter,setter.toString..方法*/}

7、测试类(简单测试)

import com.project.config.SpringConfig;import com.project.dao.AccountDao;import com.project.domain.Account;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import java.util.List;public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); AccountDao bean = ctx.getBean(AccountDao.class); Account beanById = bean.findById(1); System.out.println(beanById); }}控制台打印结果:Account{
id=1, name='1231', money=121.0}

总结:

将Spring核心配置文件中所有内容迁移到Spring核心配置类中,有需要的都交给Spring容器管理

转载地址:https://blog.csdn.net/drug1910951117/article/details/107379042 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Spring整合MyBatis--快速入门
下一篇:Spring-IOC知识点整理

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年03月24日 01时15分00秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章