首先导入依赖
1 <properties> 2 <!-- 版本锁定 --> 3 <spring.version>5.0.2.RELEASE</spring.version> 4 <log4j.version>1.2.17</log4j.version> 5 <slf4j.version>1.6.6</slf4j.version> 6 <mysql.version>5.1.47</mysql.version> 7 <mybatis.version>3.4.6</mybatis.version> 8 </properties> 9 10 <dependencies> 11 <!--Spring:ioc和aop,事务--> 12 <dependency> 13 <groupId>org.springframework</groupId> 14 <artifactId>spring-context</artifactId> 15 <version>${spring.version}</version> 16 </dependency> 17 <dependency> 18 <groupId>org.springframework</groupId> 19 <artifactId>spring-jdbc</artifactId> 20 <version>${spring.version}</version> 21 </dependency> 22 <dependency> 23 <groupId>org.aspectj</groupId> 24 <artifactId>aspectjweaver</artifactId> 25 <version>1.8.9</version> 26 </dependency> 27 28 <!--Mybatis--> 29 <dependency> 30 <groupId>mysql</groupId> 31 <artifactId>mysql-connector-java</artifactId> 32 <version>${mysql.version}</version> 33 </dependency> 34 <dependency> 35 <groupId>com.alibaba</groupId> 36 <artifactId>druid</artifactId> 37 <version>1.1.19</version> 38 </dependency> 39 <dependency> 40 <groupId>org.mybatis</groupId> 41 <artifactId>mybatis</artifactId> 42 <version>${mybatis.version}</version> 43 </dependency> 44 <dependency> 45 <groupId>org.mybatis</groupId> 46 <artifactId>mybatis-spring</artifactId> 47 <version>2.0.2</version> 48 </dependency> 49 50 <!--SpringMVC,jstl,json转换--> 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-webmvc</artifactId> 54 <version>${spring.version}</version> 55 </dependency> 56 <dependency> 57 <groupId>com.fasterxml.jackson.core</groupId> 58 <artifactId>jackson-databind</artifactId> 59 <version>2.9.9</version> 60 </dependency> 61 <dependency> 62 <groupId>javax.servlet</groupId> 63 <artifactId>javax.servlet-api</artifactId> 64 <version>4.0.1</version> 65 <scope>provided</scope> 66 </dependency> 67 <dependency> 68 <groupId>javax.servlet.jsp</groupId> 69 <artifactId>javax.servlet.jsp-api</artifactId> 70 <version>2.3.1</version> 71 <scope>provided</scope> 72 </dependency> 73 <dependency> 74 <groupId>jstl</groupId> 75 <artifactId>jstl</artifactId> 76 <version>1.2</version> 77 </dependency> 78 79 <!--日志--> 80 <dependency> 81 <groupId>log4j</groupId> 82 <artifactId>log4j</artifactId> 83 <version>${log4j.version}</version> 84 </dependency> 85 <dependency> 86 <groupId>org.slf4j</groupId> 87 <artifactId>slf4j-api</artifactId> 88 <version>${slf4j.version}</version> 89 </dependency> 90 <dependency> 91 <groupId>org.slf4j</groupId> 92 <artifactId>slf4j-log4j12</artifactId> 93 <version>${slf4j.version}</version> 94 </dependency> 95 96 <!--测试--> 97 <dependency> 98 <groupId>junit</groupId> 99 <artifactId>junit</artifactId> 100 <version>4.12</version> 101 <scope>test</scope> 102 </dependency> 103 <dependency> 104 <groupId>org.springframework</groupId> 105 <artifactId>spring-test</artifactId> 106 <version>${spring.version}</version> 107 <scope>test</scope> 108 </dependency> 109 </dependencies>
先按照Maven创建Mybatis方式进行
先创建Mybatis的主配置文件
Mybatis_config.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> <properties resource="db.properties"/> <!--配置别名,pojo包中的类都用自己的类名当别名--> <typeAliases> <package name="com.wz.pojo"/> </typeAliases> <environments default="dataSource"> <environment id="dataSource"> <transactionManager type="jdbc"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${db.driver}"/> <property name="url" value="${db.url}"/> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/UserMapper.xml"/> </mappers> </configuration>
然后是pojo类,一个简单的user
创建接口类UserMapper.java
package com.wz.Mapper; import com.wz.pojo.User; import java.util.List; public interface UserMapper { public List<User> queryAll(); }
Mybatis的映射文件
<?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.wz.Mapper.UserMapper"> <select id="queryAll" resultType="User"> select * from t_user </select> </mapper>
再来一个工具类
package com.wz.Utile; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class SqlSessionUtile { public static SqlSession sqlSession; public static SqlSession getSession(){ InputStream resource = null; try { resource = Resources.getResourceAsStream("Mybatis_config.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resource); sqlSession = sessionFactory.openSession(true); } catch (IOException e) { throw new RuntimeException(e); } return sqlSession; } public static void close(){ sqlSession.close(); } }
在测试类中测试应该是没有问题的。
@Test public void test01(){ SqlSession session = SqlSessionUtile.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); List<User> users = mapper.queryAll(); for (User user : users) { System.out.println(user); } SqlSessionUtile.close(); }
接下来就是Mybatis与Spring的整合了
创建spring的xml文件Spring-dao.xml
<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" 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 "> <context:property-placeholder location="classpath:db.properties"/> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.wz.Mapper"/> </bean> <!-- 配置SqlSessionFactoryBean对象 dataSource为数据库连接池对象 configLocation为Mybatis的主配置文件路径,用classpath前缀 --> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="typeAliasesPackage" value="com.wz.pojo"/> <property name="configLocation" value="classpath:Mybatis_config.xml"/> </bean> <!-- 创建数据库连接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${db.driver}"/> <property name="url" value="${db.url}"/> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> </bean> </beans>
这个配置文件中的
com.alibaba.druid.pool.DruidDataSource为数据库连接池,配置好数据库就行,和Mybatis的主配置里的几乎一样
注入的
SqlSessionFactoryBean有一点要说
就是configLoaction这里要配置上Mybatis的主配置路径。
标签:Spring,version,整合,spring,Mybatis,org,import,public From: https://www.cnblogs.com/auhzheng/p/17093362.html