Mybatis框架,使用代理方式整合spring

    xiaoxiao2022-07-07  200

    整合思路

    SqlSessionFactory对象应该放到spring容器中作为单例存在。Mapper代理形式中,应该从spring容器中直接获得mapper的代理对象。数据库的连接以及数据库连接池事务管理都交给spring容器来完成。

    整合需要的jar包

    链接:https://pan.baidu.com/s/1YY_jYVVkBzRxV0O6Kdvz-g  提取码:fz8k  复制这段内容后打开百度网盘手机App,操作更方便哦

    spring的jar包Mybatis的jar包Spring+mybatis的整合包。Mysql的数据库驱动jar包。数据库连接池的jar包。

    项目结构

     

    llog4j.properties

    ### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ### log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=c\:mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### # error warn info debug trace log4j.rootLogger= info, stdout

    jdbc.properties

    jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true jdbc.username=root jdbc.password=123 #jdbc.driver=com.mysql.jdbc.Driver #jdbc.url=jdbc:oracle://localhost:3306/hibernate #jdbc.username=root #jdbc.password=1234 #jdbc.driver=com.mysql.jdbc.Driver #jdbc.url=jdbc:db2://localhost:3306/hibernate #jdbc.username=root #jdbc.password=1234

    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:jdbc.properties"/> <!--数据库连接池 --> <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> <!-- mapper配置 --> <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--数据库连接池 --> <property name="dataSource" ref="dataSource"></property> <!-- 别名 --> <property name="typeAliasesPackage" value="com.it.mapper"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.it.mapper"></property> </bean> </beans>

    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.it.mapper.UserMapper"> <!-- statementId --> <select id="findById" parameterType="int" resultType="com.it.bean.User"> select * from user where id = #{id} </select> </mapper>

    UserMapper.java

    package com.it.mapper; import com.it.bean.User; public interface UserMapper { //Mapper接口方法名和Mapper.xml中定义的statement的id相同 //Mapper接口方法的输入参数类型和mapper.xml中定义的statement的parameterType的类型相同 //Mapper接口方法的输出参数类型和mapper.xml中定义的statement的resultType的类型相同 //接口和映射文件的名字最好保持一致 UserMapper.java UserMapper.xml //接口和映射文件最好放到同一个目录 public User findById(int id); }

    user.java

    package com.it.bean; import java.util.Date; public class User { private int id; private String username;// 用户姓名 private String sex;// 性别 private Date birthday;// 生日 private String address;// 地址 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 getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address + "]"; } }

    测试

    package com.it.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.it.bean.User; import com.it.mapper.UserMapper; public class Test { @SuppressWarnings("resource") public static void main(String[] args) { // 1、加载spring容器|初始化容器 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 2、从容器中获取对象 UserMapper bean = context.getBean(UserMapper.class); User user = bean.findById(1); System.out.println(user); } }

     

     

    最新回复(0)