mybatis与spring的整合(代码实现)

时间:2024-01-11 10:51:08

                mybatis与spring的整合(代码实现)

需要jar包:

    mybatis核心包;依赖包;log4j包;spring croe;beans;tx;aop;aspects;context;expression;jdbc; mybatis和spring得整合jar包;

  环境配置:

    1)mybatis.xml和mapper.xml配置在第一节有

    2)spring.xml配置在第二节有

  当配置好所有得环境,引入jar包后,开始写代码。

    1.先配置mybatis.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "http://mybatis.org/dtd/mybatis-3-config.dtd" "mybatis-3-config.dtd" >
<configuration>
<typeAliases>
<package name="com.bean" />
</typeAliases>
</configuration>

       2.现在用spring.xml配置,-->开启自动扫描(注释)-->properties文件得placeholder-->连接数据库

<context:component-scan base-package="com.service" />
<context:property-placeholder location="classpath:db.properties" />
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${Adriver}"></property>
<property name="JdbcUrl" value="${Aurl}"></property>
<property name="user" value="${Ausername}"></property>
<property name="password" value="${Apassword}"></property>
<property name="maxPoolSize" value="10"></property>
<property name="initialPoolSize" value="3"></property>
</bean>

      3.写一个bean(UsersBean)

package com.bean;

public class UsersBean {

    private int uid;
private String uname;
private String upass; public int getUid() {
return uid;
} public void setUid(int uid) {
this.uid = uid;
} public String getUname() {
return uname;
} public void setUname(String uname) {
this.uname = uname;
} public String getUpass() {
return upass;
} public void setUpass(String upass) {
this.upass = upass;
} }

      4.写一个UsersDao,对数据库数据进行操作

package com.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.bean.UsersBean;

public interface IUsersDao {

    public List<UsersBean> queryAll();
}

      5.写一个mapper.xml映射文件(IUsersDao.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "http://mybatis.org/dtd/mybatis-3-mapper.dtd" "mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.IUsersDao">
<select id="queryAll" resultType="usersbean">
select * from users
</select>
</mapper>

    6.spring.xml配置,sqlSessionFactory()创建,mapper.xml文件得映射

    

<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
<property name="dataSource" ref="datasource"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao"></property>
<property name="sqlSessionFactoryBeanName" value="factory"></property>
</bean>

    7.UsersServiceImpl类继承IUsersService ,其中:(@Resource(name="IUsersDao"))name属性是值service层和dao层得关联

一般如果dao层mapper.xml文件名为UserDao,则name="userDao",不写就是默认为userDao.但我这里得name="IUserDao".

package com.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bean.UsersBean;
import com.dao.IUsersDao;
import com.service.IUsersService; @Service
public class UsersServiceImpl implements IUsersService { @Resource
private IUsersDao usersDao; @Override
public List<UsersBean> findAll() {
// TODO Auto-generated method stub
return usersDao.queryAll();
}
}

        在这里遇到了一个问题 ,在spring.xml中我写的是批量配置使用了SqlSessionFactoryBean   本@(Resource name="userDao")第一个字母小写,可能是name="iUserDao"或者name="IuserDao",但都是错误得,因为我用的是以I开头的接口名,然后(@Resource(name="IUsersDao")是这样的。这是一个问题我不太懂,不懂它底层是怎样实现,希望大家懂得可以留言,我们逐一讨论。

  8.测试;(junit)

package test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bean.UsersBean;
import com.service.impl.UsersService; class Test { @org.junit.jupiter.api.Test
void test() {
// 实例化UsersService对象,调用显示方法
ApplicationContext app = new ClassPathXmlApplicationContext("spring/spring.xml");
UsersService us = app.getBean("UsersService", UsersService.class);
List<UsersBean> list = us.findAll();
for (UsersBean user : list) {
if (list != null) {
System.out.println("uid:" + user.getUid() + "\tuname:" + user.getUname() + "\tupwd:" + user.getUpwd());
}
}
System.out.println(app);
}
}

    ok!!

    多做笔记,加深记忆!!!

    学无止境。