spring+mybatis整合
首先我先给大家看下我的工程目录:

我下面的讲解中就不说明文件创建在哪里了 创建位置可以这张图
需要用的的jar包大家可以从这里下载
链接:https://pan.baidu.com/s/1geHZhRH 密码:vevy
(我用的是sqlserver如果你们用的不是需要下载下你们用的数据库的驱动包)
接下来就要进入我们严峻的写代码环节
一、创建一个 java或者web工程 因为没有用到web所有都可以
二、在数据库中创建一个表(这个比较简单大家可以根据自己的喜好创建一个表)
贴上一张我的
三、将我们下载好的jar包放到lib下面(如果没有lib可以 自行创建)然后build一下(一定要build)
四、建立JDBC属性文件 db.properties
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver jdbc.url=jdbc:sqlserver://localhost:1433;databaseName=Demo jdbc.username=sa jdbc.password=yi19950816
五、创建spring和mybatis的整合文件appilcationContast.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:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" > <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- sqlSessinFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" > <!-- 加载mybatis的配置文件 --> <property name="configLocation" value="mybatis/SqlMayConfigl.xml"></property> <!-- 数据源 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- mapper的批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且 在spring容器中注册 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定扫描的包名 如果扫描多个包 每个包中用,隔开--> <property name="basePackage" value="com.ssm.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
六、创建Log4j的配置 log4j.properties
log4j.rootLogger=DEBUG,stdout log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern =[%-5p] %d{yyyy-MM-dd HH\:mm\:ss,SSS} method\:%l%n%m%n
七、SqlMayConfigl.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> <!-- 加载影射文件 --> <mappers> <mapper resource="sqlMapper/UserMapper.xml"/> </mappers> </configuration>
八、UserMapper.xml
<?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="test"> <!-- 在影射文件中查询 --> <!--id:是 用来标识文件中的SQL #{id}: #{}:表示一个占位符 id:表示输入的 参数 parameterType:指定输入参数的类型 resultType:输出sql结果的类型--> <select id="findUserById" parameterType="int" resultType="com.ssm.po.User"> SELECT * FROM Demo4 WHERE id=#{id} </select> </mapper>
九、User.java
package com.ssm.po; public class User { @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + "]"; } //属性名和数据库表的字段要对应 private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
十、UserMapper.java
package com.ssm.mapper; import com.ssm.po.User; public interface UserMapper { public User findUserById(int id) throws Exception; }
十一、UserMapper.xml
<?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.ssm.mapper.UserMapper"> <select id="findUserById" parameterType="int" resultType="com.ssm.po.User"> SELECT * FROM Demo4 WHERE id=#{id} </select> </mapper>
十二、创建一个测试文件测试我们的代码
package com.ssm.mapper; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ssm.po.User; public class UserMapperTest { private ApplicationContext a; @Before public void seyUp() throws Exception { a=new ClassPathXmlApplicationContext("classpath:spring/appilcationContast.xml"); } @Test public void testFindUserById() throws Exception { UserMapper userMapper=(UserMapper) a.getBean("userMapper"); User user=userMapper.findUserById(1); System.out.println(user); } }
十三、运行testFindUserById()显示

运行结果显示 User [id=1, username=admin , password=123456 ]表示我们spring和mybatis整合是正确的

浙公网安备 33010602011771号