欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring 集成 mybatis的实例详解

程序员文章站 2022-04-14 09:41:24
环境配置1>先创建maven的quickstart项目;并且创建dao层,service层,controller层,po层,mapper,resources以及下面的配置文件(db.proper...

 环境配置

1>先创建maven的quickstart项目;并且创建dao层,service层,controller层,po层,mapper,resources以及下面的配置文件(db.properties,log4j.properties,mybatis.xml,spring.xml).

spring 集成 mybatis的实例详解

2>配置pom.xml

修改jdk版本;

添加依赖:

​ junit版本改为4.12;spring-context;spring-test;spring-jdbc;spring-tx(事务);aspectjweaver(切面编程);c3p0(连接池);mybatis;mybatis-spring;mysql-connector-java(mysql驱动包);slf4j-log4j12,slf4j-api(日志打印);

设置资源目录和插件

 <build>
 <!--
  maven 项目:如果源代码(src/main/java)存在xml、properties、tld 等文件
  maven 默认不会自动编译该文件到输出目录,如果要编译源代码中xml properties tld 等文件
  需要显式配置 resources 标签
 -->
 <resources>
  <resource>
  <directory>src/main/resources</directory>
  </resource>
  <resource>
  <directory>src/main/java</directory>
  <includes>
   <include>**/*.xml</include>
   <include>**/*.properties</include>
   <include>**/*.tld</include>
  </includes>
  <filtering>false</filtering>
  </resource>
 </resources>
 </build>

3>配置spring.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
  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
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx.xsd">
  <!-- 扫描基本包 -->
  <context:component-scan base-package="com.xxxx" />

  <!-- 加载properties 配置文件 -->
  <context:property-placeholder location="classpath:db.properties" />

  <!-- aop -->
  <aop:aspectj-autoproxy />

  <!-- 配置c3p0 数据源 -->
  <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>

  <!-- 配置事务管理器 -->
  <bean id="txmanager"
     class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
    <property name="datasource" ref="datasource"></property>
  </bean>

  <!-- 设置事物增强 -->
  <tx:advice id="txadvice" transaction-manager="txmanager">
    <tx:attributes>
      <tx:method name="add*" propagation="required" />
      <tx:method name="insert*" propagation="required" />
      <tx:method name="update*" propagation="required" />
      <tx:method name="delete*" propagation="required" />
    </tx:attributes>
  </tx:advice>

  <!-- aop 切面配置 -->
  <aop:config>
    <aop:pointcut id="servicepointcut" expression="execution(* com.xxxx.service..*.*(..))" />
    <aop:advisor advice-ref="txadvice" pointcut-ref="servicepointcut" />
  </aop:config>

  <!-- 配置 sqlsessionfactory -->
  <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
    <property name="datasource" ref="datasource"></property>
    <property name="configlocation" value="classpath:mybatis.xml" />
    <property name="mapperlocations" value="classpath:com/xxxx/mapper/*.xml" />
  </bean>

  <!-- 配置扫描器 -->
  <bean id="mapperscanner" class="org.mybatis.spring.mapper.mapperscannerconfigurer">
    <!-- 扫描com.xxxx.dao这个包以及它的子包下的所有映射接口类 -->
    <property name="basepackage" value="com.xxxx.dao" />
    <property name="sqlsessionfactorybeanname" value="sqlsessionfactory" />
  </bean>
</beans>

4>配置 mybatis.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.xxxx.po"/>
 </typealiases>
</configuration>

5>配置 db.properties

jdbc.url中?前面的spring_mybatis是数据库名字,注意要修改下

password是密码,也是要修改下的

6>添加日志

jdbc.driver=com.mysql.cj.jdbc.driver
jdbc.url=jdbc:mysql://localhost:3306/spring_mybatis?
useunicode=true&characterencoding=utf8&servertimezone=gmt%2b8&usessl=false
jdbc.username=root
jdbc.password=root 

log4j.properties

log4j.rootlogger=debug, console
# console
log4j.appender.console=org.apache.log4j.consoleappender
log4j.appender.console.layout=org.apache.log4j.patternlayout
log4j.appender.console.layout.conversionpattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.resultset=info
log4j.logger.org.apache=info
log4j.logger.java.sql.connection=debug
log4j.logger.java.sql.statement=debug
log4j.logger.java.sql.preparedstatement=debug

添加源代码

1>在po 包下创建 javabean 文件 user.java

public class user {
 private integer userid;
 private string username;
 private string userpwd;
 private string useremail;
 private date createdate;
 private date updatedate;
 
 /**
  set get tostring 方法省略
 **/
}

2>在dao层添加userdao接口

public interface userdao {
 user queryuserbyuserid(integer userid);
}

3>在mapper包添加usermapper.xml 映射文件

sql代码写在这地方

<?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.xxxx.dao.userdao">
  <select id="queryuserbyuserid" parametertype="int"
resulttype="com.xxxx.po.user">
   select user_id as userid,user_name as username,user_pwd as userpwd
   from tb_user
   where user_id = #{userid}
  </select>
</mapper>

4>添加 userservice.java

@service
public class userservice {
 @autowired
 private userdao userdao;
 public user queryuserbyuserid(integer userid){
  return userdao.queryuserbyuserid(userid);
 }
}

5>添加 usercontroller.java

@controller
public class usercontroller {
 // 注入userservice
 @resource
 private userservice userservice;
 /**
 * 通过用户id查询用户对象
 * @param userid
 * @return
 */
 public user queryuserbyuserid(integer userid) {
  user user = userservice.queryuserbyuserid(userid);
  return user;
 }
}

执行测试

public class app {
 public static void main(string[] args) {
  // 加载spring的配置
  beanfactory factory = new classpathxmlapplicationcontext("spring.xml");
  // 得到usercontroller对象
  usercontroller usercontroller = (usercontroller)
factory.getbean("usercontroller");
  // 调用方法
  user user = usercontroller.queryuserbyuserid(1);
system.out.println(user.tostring());
 }
}

到此这篇关于spring 集成 mybatis的文章就介绍到这了,更多相关spring 集成 mybatis内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!