您现在的位置是:首页 >技术杂谈 >spring集成mybatis网站首页技术杂谈

spring集成mybatis

小斯要觉觉 2024-07-02 12:01:02
简介spring集成mybatis

目录

(1)新建javaEE web项目

​(2)加入相关依赖的坐标

(3) 创建相应的包和类

(4) 配置spring和mybatis的配置文件

在resources中建mybatis-config.xml

在 resources中建spring.xml

在 resources中建db.xml

在 resources中建config.propertis

集成mybatis配置 ,导入mybatis-spring  的jar包   

Spring 结合 mybatis 插件包

在 resources中建spring-mybatis.xml

 搭建完成后测试一下


ssm(spring, springweb, mybatis)

(1)新建javaEE web项目

 

 (2)加入相关依赖的坐标

数据访问:mybatis, mysql, junit, druid, 

spring框架: spring-context, spring-jdbc, spring-aop

<!-- spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.2</version>
</dependency>
<!-- spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.2</version>
</dependency>
<!--  spring-aop -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.3.2</version>
</dependency>
<!-- 阿里数据源 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>
<!--  mysql-connector -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.16</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!-- 单元测试-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>provided</scope>
</dependency>

 (3) 创建相应的包和类

package com.ffyc.ssm.dao;

import com.ffyc.ssm.model.Admin;
import org.springframework.stereotype.Repository;

@Repository
public interface LoginDao {


    public Admin login(Admin admin);
}
package com.ffyc.ssm.model;

public class Admin {
    private int id;
    private String account;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
package com.ffyc.ssm.service;

import com.ffyc.ssm.dao.LoginDao;
import com.ffyc.ssm.model.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service(value = "loginService")
public class LoginService {

    @Autowired
    LoginDao loginDao;
    public Admin login(Admin admin){
       Admin a = loginDao.login(admin);
        return a;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--
  映射文件与操作接口绑定的
-->

<mapper namespace="com.ffyc.ssm.dao.LoginDao">


    <select id="login" resultType="com.ffyc.ssm.model.Admin">
        select * from admin where account = #{account} and password = #{password}
    </select>
</mapper>

(4) 配置spring和mybatis的配置文件

在resources中建mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--
这是mybatis核心配置文件
-->

    <settings>
         <!--启用日志功能,在运行时,可以将实际执行的sql细节打印到控制台-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <!-- 配置类型简称   -->
    <typeAliases>
    <!-- <typeAlias type="com.ffyc.mybatispro.model.Admin" alias="Admin"></typeAlias>-->
    <package name="com.ffyc.ssm.model"/>
    </typeAliases>



</configuration>

在 resources中建spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--  开启注解扫描  -->
    <context:component-scan base-package="com.ffyc.ssm"> </context:component-scan>
    <!--  开启自动代理  -->
    <aop:aspectj-autoproxy />
    <!--  导入数据库连接,以及事务管理配置   -->
    <import resource="classpath:db.xml"></import>

</beans>

在 resources中建db.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      https://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--用来配置数据库相关的东西-->
    <!--导入属性文件-->
    <context:property-placeholder location="classpath:config.properties"></context:property-placeholder>
    <!--管理数据源对象-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${classDriverName}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${uname}"></property>
        <property name="password" value="${pwd}"></property>
        <property name="initialSize" value="5"></property>
        <property name="maxActive" value="20"></property>
    </bean>

    <!-- 配置 spring 事务管理类, 并注入数据源 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 开启注解事务管理 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

在 resources中建config.propertis

classDriverName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/ssmdb?serverTimezone=Asia/Shanghai
uname=root
pwd=xiaosi

集成mybatis配置 ,导入mybatis-spring  的jar包   

Spring 结合 mybatis 插件

< dependency >
< groupId >org.mybatis</ groupId >
< artifactId >mybatis-spring</ artifactId >
< version >1.3.1</ version >
</ dependency >

在 resources中建spring-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">



    <!--  导入数据库连接,以及事务管理配置   -->
    <import resource="classpath:db.xml"></import>



    <!--    让spring框架配置 sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--   注入数据源     -->
        <property name="dataSource" ref="dataSource"></property>
        <!--   配置mybatis文件     -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!--   扫描sql映射文件     -->
        <property name="mapperLocations" value="classpath:mappers/*Mapper.xml">
        </property>
    </bean>


    <!--  让spring框架指定生成接口代理-->
    <bean id="mapperFactory" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ffyc.ssm.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">
        </property>
    </bean>
</beans>

 搭建完成后测试一下

package com.ffyc.ssm.test;

import com.ffyc.ssm.model.Admin;
import com.ffyc.ssm.service.LoginService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test1 {
    @Test
    public void test(){
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("spring.xml");
        LoginService loginService = app.getBean("loginService", LoginService.class);
        Admin admin = new Admin();
        admin.setAccount("xiaosi");
        admin.setPassword("999");
        Admin login = loginService.login(admin);
        System.out.println(login);
    }
}

测试结果: 

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。