mybatis整合spring
mybatis 整合spring需要的jar包

需要4个配置文件 放在src下即可

applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 数据库配置文件路径 --> <context:property-placeholder location="classpath:db.properties"/> <!-- dbcp数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <!-- mybatis工厂 --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--核心配置文件位置 --> <property name="configLocation" value="classpath:SqlMapConfig.xml "/> </bean> <!-- mapper动态代理开发 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="mapper"></property> </bean> </beans>
db.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/schoolpro?characterEncoding=utf-8 jdbc.username=root jdbc.password=123456
log4j.properties
# Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
SqlMapConfig.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="pojo"/> </typeAliases> </configuration>
在src下创建pojo包 pojo下创建实体类和包装类
SysUser.java
package pojo;
public class SysUser {
private static final long serialVersionUID = 1L;
private Integer Id;
private String SysUserName;//管理员名字
private String PassWord;//管理员密码
@Override
public String toString() {
return "SysUser [Id=" + Id + ", SysUserName=" + SysUserName + ", PassWord=" + PassWord + "]";
}
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
public String getSysUserName() {
return SysUserName;
}
public void setSysUserName(String sysUserName) {
SysUserName = sysUserName;
}
public String getPassWord() {
return PassWord;
}
public void setPassWord(String passWord) {
PassWord = passWord;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
SU.java
package pojo;
import java.io.Serializable;
public class SU implements Serializable{
private static final long serialVersionUID = 1L;
private SysUser sysuser;
public SysUser getSysuser() {
return sysuser;
}
public void setSysuser(SysUser sysuser) {
this.sysuser = sysuser;
}
}
在src 下创建Mapper包 在下创建mapper接口
SysUserMapper.java
package mapper;
import pojo.SysUser;
public interface SysUserMapper {
//Mapper接口需要遵循的四个原则
//接口方法名== SysUser.xml中Id名
//返回值类型与Mapper.xml文件中返回值类型要一致
//方法的入参类型与Mapper.xml中的入参的类型要一致
public SysUser findUserById(Integer id);
}
在src下创建sqlmap包 下创建SysUser.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"> <!-- 写sql语句 --> <!-- namespace为命名空间 由于跟Spring整合后需填写Mapper接口路径 --> <mapper namespace="mapper.SysUserMapper"> <!-- 通过ID查询一个用户 --> <select id="findUserById" parameterType="Integer" resultType="SysUser"> select * from sysUser where id = #{v} </select> </mapper>
再在SqlMapConfig.xml下添加map

然后写个测试类测试下是否整合成功
package junit; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import mapper.SysUserMapper; import pojo.SysUser; public class mybatisTest { @Test public void testmybatis() throws Exception{ //加载核心配置文件 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); SysUserMapper sysusermapper = ac.getBean(SysUserMapper.class); SysUser sysuser=sysusermapper.findUserById(4); System.out.println(sysuser); } }
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 数据库配置文件路径 --><context:property-placeholder location="classpath:db.properties"/><!-- dbcp数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="maxActive" value="10" /><property name="maxIdle" value="5" /></bean><!-- mybatis工厂 --><bean id="sqlSessionFactoryBean"class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><!--核心配置文件位置 --><property name="configLocation" value="classpath:SqlMapConfig.xml "/></bean><!-- mapper动态代理开发 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="mapper"></property></bean></beans>

浙公网安备 33010602011771号