mybatis与spring的整合

本文介绍了MyBatis与Spring框架整合的两种方法:原始DAO方法和代理方式,并详细展示了如何搭建整合环境,包括配置文件、接口定义及实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mybatis与spring整合

方式1:通过原始dao方法(不使用代理),

方式2:通过代理方式

搭建运行环境

1)导入

mybatis的jar包--->源码中的核心包+依赖包

mybatis提供的与spring整合的包

spring的核心包+事务包+aop面向切面编程包+数据库jdbc包

数据库驱动+c3p0包

2)建立配置文件 包括spring的核心文件bean.xml 以及 mybatis的核心文件sqlMapConfig.xml 


2.User

public class User {
	private int id;
	private String username;
	private Date birthday;
	private String sex;//省略getter setter

============================================普通Dao方式==========================================================================

3.配置文件User.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">
<!-- 这里的namespace命名空间,等于mapper接口的地址 -->
<mapper namespace="test">
	<select id="findUserById" parameterType="int" resultType="cn.itcast.domain.User">
		select * from user where id=#{id}
	</select>
</mapper>


4.UserDao

package cn.itcast.dao;

import java.util.List;

import cn.itcast.domain.User;

public interface UserDao {
	public User findUserById(int id);
}
5.UserDaoImp

实现SqlSessionDaoSupport接口就不用注入SqlSessionFactory了 因为这个接口中已经定义了 并且有setter方法

package cn.itcast.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.itcast.dao.UserDao;
import cn.itcast.domain.User;
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{
	public User findUserById(int id) {
		SqlSession sqlSession = this.getSqlSession();
		User user=sqlSession.selectOne("test.findUserById", id);
		return user;
	}


}

============================================代理方式整合==========================================================================

下面是代理方式配置:

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">
<!-- 这里的namespace命名空间,等于mapper接口的地址 -->
<mapper namespace="cn.itcast.mapper.UserMapper">
	<select id="findUserById" parameterType="int" resultType="cn.itcast.domain.User">
		select * from user where id=#{id}
	</select>
</mapper>
UserMapper接口

package cn.itcast.mapper;
import cn.itcast.domain.User;
public interface UserMapper {
	public User findUserById(int id);
}

核心配置文件sqlMapperConfig.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>
		<!-- 1.通过resource方法一次加载一个映射文件 -->
		<mapper resource="cn/itcast/mapper/User.xml" />
		<mapper resource="cn/itcast/mapper/UserMapper.xml" />
	</mappers>
</configuration>

bean..xml文件

1)数据源的配置

2)sqlSessionFactory的配置

<?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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
     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
        ">
        <!-- 1.配置数据源连接池 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis01"></property>
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="user" value="root"></property>
		<property name="password" value="169500"></property>
		<property name="initialPoolSize" value="3"></property>
		<property name="maxPoolSize" value="6"></property>
		</bean>
        <!--2. 配置sqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        	<!-- 加载sqlMapConfig.xml文件 -->
        	<property name="configLocation" value="sqlMapConfig.xml"></property>
        	<!-- 数据源 -->
        	<property name="dataSource" ref="dataSource"></property>
        </bean>
        <bean id="userDao" class="cn.itcast.dao.impl.UserDaoImpl"><!--普通Dao方式配置-->
        	<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
        </bean>
        <!-- mapper代理方式配置 -->
        <!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        	mapperInterface
        	<property name="mapperInterface" value="cn.itcast.mapper.UserMapper"></property>
        	<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
        </bean> -->
        <!-- mapper代理配置包扫描的方式 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        	<!-- 指定扫描的包 -->
        	<property name="basePackage" value="cn.itcast.mapper"></property>
        	<!-- sqlSessionFactory的配置 -->
        	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        </bean>
</beans>
测试:

package cn.itcast.test;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.dao.UserDao;
import cn.itcast.domain.User;
import cn.itcast.mapper.UserMapper;

public class Spring_Mybatis_Test {
	@Test
	public void test(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
		UserDao userDao=(UserDao) ac.getBean("userDao");
		User user = userDao.findUserById(2);
		System.out.println(user.getAddress());
	}@Test
	public void testmapper(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
		UserMapper userMapper= (UserMapper) ac.getBean("userMapper");
		User user = userMapper.findUserById(2);
		System.out.println(user.getAddress());
	}
}






基于数据挖掘的音乐推荐系统设计实现 需要一个代码说明,需要论文 采用python语言,django框架,mysql数据库开发 编程环境:pycharm,mysql8.0 系统分为前台+后台模式开发 网站前台: 用户注册, 登录 搜索音乐,音乐欣赏(可以在线进行播放) 用户登陆时选择相关感兴趣的音乐风格 音乐收藏 音乐推荐算法:(重点) 本课题需要大量用户行为(如播放记录、收藏列表)、音乐特征(如音频特征、歌曲元数据)等数据 (1)根据用户之间相似性或关联性,给一个用户推荐其相似或有关联的其他用户所感兴趣的音乐; (2)根据音乐之间的相似性或关联性,给一个用户推荐其感兴趣的音乐相似或有关联的其他音乐。 基于用户的推荐和基于物品的推荐 其中基于用户的推荐是基于用户的相似度找出相似相似用户,然后向目标用户推荐其相似用户喜欢的东西(和你类似的人也喜欢**东西); 而基于物品的推荐是基于物品的相似度找出相似的物品做推荐(喜欢该音乐的人还喜欢了**音乐); 管理员 管理员信息管理 注册用户管理,审核 音乐爬虫(爬虫方式爬取网站音乐数据) 音乐信息管理(上传歌曲MP3,以便前台播放) 音乐收藏管理 用户 用户资料修改 我的音乐收藏 完整前后端源码,部署后可正常运行! 环境说明 开发语言:python后端 python版本:3.7 数据库:mysql 5.7+ 数据库工具:Navicat11+ 开发软件:pycharm
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值