在Maven中如何成功整合Spring-Mybatis-----这里很详细

tech2024-02-23  69

Spring-Mybatis配置
1. Maven依赖配置

在Maven项目中需要 在pom.xml中导入依赖

<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.5</version> </dependency> <!--spring操作数据需要spring-jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.7.RELEASE</version> </dependency> </dependencies>
2. Resources文件配置
2.1 Spring-Dao.xml配置

将Spring与Mybatis整合在一起的配置,具有数据驱动,配置SqlSessionFactory对象等

<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--dataSource 使用spring数据源替换mybatis配置 c3p0\dbcp\druid class使用spring提供的jbdc DriverManagerDataSource驱动管理数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!--配置SqlSessionFactory对象--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!--绑定mybatis配置文件--> <property name="configLocation" value="classpath:Mybatis-Config.xml"/> <property name="mapperLocations" value="classpath:com/muzi/Mapper/*.xml"/> </bean> <!--SqlSessionTemplate:就是SqlSession我们使用的--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> <!--只能使用构造器注入sqlSessionFactory没有set方法--> </bean> <!--配置声明式事务--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <constructor-arg ref="dataSource" /> </bean> <!--结合aop实现事物的织入--> <!--配置事务通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!--配置事务传播特性 propagation--> <tx:method name="insert"/> <tx:method name="select" propagation="REQUIRED"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!--配置事务切入点--> <aop:config> <!--mapper下面的所有类,所有方法--> <aop:pointcut id="txPoincut" expression="execution(* com.muzi.Mapper.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoincut"/> </aop:config> </beans>
2.2 Mybatis-Configer.xml配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--设置别名--> <typeAliases> <package name="com.muzi.pojo"/> </typeAliases> </configuration>
2.3 database.properties配置
jdbc.driver=com.mysql.jdbc.Driver #mysql8时区问题"jdbc:mysql://localhost:3306/db2?serverTimezone=UTC" jdbc.url=jdbc:mysql://localhost:3306/SSMBuilb/?useSSL=true&useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=root
2.4 Application.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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="Spring-dao.xml"/> <bean id="userMapper" class="com.muzi.Mapper.UserMapperImp"> <property name="sqlSession" ref="sqlSession"/> </bean> </beans>
3. 静态资源导出
<!-- maven,在build中配置resources,防止一些资源导出失败的问题--> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
最新回复(0)