学会Spring整合Mybatis,轻松提升Java项目效率!

文摘   2024-12-06 22:00   江苏  

桥哥我又来啦!今天咱们要盘一盘Spring和Mybatis这对黄金搭档。Spring负责管理对象,Mybatis负责数据库操作,强强联手,天下我有!整合这两个框架,就像给你的Java项目装上了涡轮增压,效率嗖嗖的!

准备工作:导入依赖

首先,得把需要的“零件”准备好,也就是各种依赖库。在pom.xml文件中,把Spring和Mybatis相关的依赖都加上,就像组装电脑一样,螺丝钉一个都不能少。

ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(line<!-- 其他依赖 --><dependency>    <groupId>org.mybatis</groupId>    <artifactId>mybatis-spring</artifactId>    <version>...</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-context</artifactId>    <version>...</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-jdbc</artifactId>    <version>...</version></dependency><!-- 其他依赖 -->

创建POJO类:实体化数据

有了依赖,接下来就要创建POJO类,也就是普通的Java对象。这些类对应数据库中的表,把数据库里的数据变成Java对象,方便操作。比如数据库里有个user表,就得创建一个User类,里面有idusernamepassword等属性。

ounter(lineounter(lineounter(lineounter(lineounter(lineounter(linepublic class User {    private int id;    private String username;    private String password;    // getter和setter方法}

创建Dao接口:数据库操作入口

Dao接口定义了对数据库的操作,比如增删改查。用注解的方式,可以省去不少配置。

ounter(lineounter(lineounter(lineounter(lineounter(linepublic interface UserDao {    @Select("SELECT * FROM user WHERE id = #{id}")    User getUserById(int id);    // 其他数据库操作方法}

创建Service接口和实现类:业务逻辑层

Service层处理业务逻辑,调用Dao层的方法来操作数据库。

ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(linepublic interface UserService {    User getUserById(int id);}@Servicepublic class UserServiceImpl implements UserService {    @Autowired    private UserDao userDao;    @Override    public User getUserById(int id) {        return userDao.getUserById(id);    }}

配置文件:连接数据库

jdbc.properties文件配置数据库连接信息,就像告诉程序数据库在哪儿,用户名密码是什么。

ounter(lineounter(lineounter(lineounter(linejdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/your_database_namejdbc.username=your_usernamejdbc.password=your_password

Spring配置类:管理Bean

Spring配置类负责管理各种Bean,包括SqlSessionFactory和MapperScannerConfigurer。SqlSessionFactory是Mybatis的核心,MapperScannerConfigurer负责扫描Mapper接口。

ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(line@Configuration@MapperScan("your.mapper.package") // 扫描Mapper接口所在的包public class MybatisConfig {    @Bean    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws Exception {        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();        factoryBean.setDataSource(dataSource);        // 其他配置        return factoryBean;    }}@Configurationpublic class JdbcConfig {    @Bean    public DataSource dataSource() {        DruidDataSource dataSource = new DruidDataSource();        // 设置数据库连接信息        return dataSource;    }}

温馨提示:  注意导入javax.sql.DataSource,而不是javax.activation.DataSource,否则会掉进坑里!

运行测试:见证奇迹

最后,写个测试类,看看整合是否成功。

ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(line@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = {SpringConfig.class})public class Test {    @Autowired    private UserService userService;    @Test    public void testGetUserById() {        User user = userService.getUserById(1);        System.out.println(user);    }}

总结:Spring + Mybatis =  起飞

Spring和Mybatis的整合,关键在于SqlSessionFactory和MapperScannerConfigurer的配置。搞清楚这两个,整合就So easy!今天的分享就到这里,桥哥下次再带你飞!

夜半探案
每日一案,一案一法,一起学习生活中的法律知识。
 最新文章