22-spring+mybatis整合

1、pom

2、配置application-context.xml

3、配置jdbc.properties

4、测试

5、结果

6、把dao 包放在工厂中管理

7、把service 包放在工厂中管理

 

1、pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>untitled4</artifactId>
    <version>1.0</version>


    <dependencies>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!--  spring 依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.3.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.2.RELEASE</version>
        </dependency>

        <!--mybatis spring整合 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.3</version>
        </dependency>

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>

        <!--  mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>

    </dependencies>

    <distributionManagement>
        <repository>
            <id>releases</id>
            <url>http://localhost:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>



</project>

2、配置application-context.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 引入jdbc.properties -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" init-method="init">
        <property name="driverClassName" value="${jdbc.className}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- 配置 sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 引入数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!--指定mapper的位置-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>


   


</beans>

3、配置jdbc.properties

jdbc.user=root
jdbc.password=root
#jdbc.className=com.mysql.jdbc.Driver
jdbc.className=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/db8081?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull

4、测试

       //加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
        //获取 sqlSessionFactory
        SqlSessionFactory sqlSessionFactory =(SqlSessionFactory) context.getBean("sqlSessionFactory");
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentDaoMapper mapper = sqlSession.getMapper(StudentDaoMapper.class);
        List<Student> allStudent =mapper.getAllStudent();
        allStudent.stream().forEach(e -> System.out.println(e));

 

5、结果

 

6、上面测试过于麻烦。修改spring-context.xml。(新增dao下的扫描)  把dao包放在工厂中管理   (=======================)

 

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 引入jdbc.properties -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" init-method="init">
        <property name="driverClassName" value="${jdbc.className}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- 配置 sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 引入数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!--指定mapper的位置-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>

   <!--  新增dao 下的扫描 -->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.nuc.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>


</beans>

 

测试时:使用这个。直接扫描到dao 包下

     //加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
//studentDaoMapper 来自dao包下的小写
        StudentDaoMapper studentDaoMapper = (StudentDaoMapper)context.getBean("studentDaoMapper");
        studentDaoMapper.getAllStudent().stream().forEach(System.out::println);

 

在新增一个,直接扫描到service 包下。将service 也注册到工厂中(=======================)

 

StudentDaoMapperService
public interface StudentDaoMapperService {
    List<Student> getAllStudent();
}
StudentDaoMapperServiceIml
public class StudentDaoMapperServiceIml implements StudentDaoMapperService {

    //提供set方法  进行注入 dao 
    private StudentDaoMapper studentDaoMapper;

    public StudentDaoMapper getStudentDaoMapper() {
        return studentDaoMapper;
    }

    public void setStudentDaoMapper(StudentDaoMapper studentDaoMapper) {
        this.studentDaoMapper = studentDaoMapper;
    }

    @Override
    public List<Student> getAllStudent() {
        return studentDaoMapper.getAllStudent();
    }
}

配置spring-context.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 引入jdbc.properties -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" init-method="init">
        <property name="driverClassName" value="${jdbc.className}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- 配置 sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 引入数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!--指定mapper的位置-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>


    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.nuc.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!--    配置 service 将server 也给搞到工厂中-->
    <bean id="studentDaoMapperServiceIml" class="com.nuc.serveriml.StudentDaoMapperServiceIml">
        <property name="studentDaoMapper" ref="studentDaoMapper"></property>
    </bean>


</beans>

测试

ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
StudentDaoMapperServiceIml studentDaoMapperServiceIml = (StudentDaoMapperServiceIml)context.getBean("studentDaoMapperServiceIml");
studentDaoMapperServiceIml.getAllStudent().stream().forEach(e -> System.out.println(e));

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值