Mybatis学习之与Spring整合

bean对象定义:

package com.mybatis.bean;

public class Person {

	private int id;

	private String name;

	private int age;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

  mappper接口,通过注解方式定义SQL语句

package com.mybatis.inter;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.mybatis.bean.Person;

public interface PersonDAO {

	@Insert("insert into person(name,age) values(#{name},#{age})")
	public void add(Person person);

	@Delete("delete from person where id=#{id}")
	public void delete(int id);

	@Select("select * from person where id=#{id}")
	public Person queryById(int id);

	@Select("select * from person")
	public List<Person> findAll();

	@Update("update person set name=#{name},age=#{age} where id=#{id}")
	public void update(Person person);

}

  mybatis-config.xml配置,数据源配置转到Spring文件中,此处只声明mapper,class属性指向mapper接口

<?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>
		<mapper class="com.mybatis.inter.PersonDAO"/>
	</mappers>
</configuration>

  数据库属性文件定义persistence.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test
username=root
password=root

  Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:util="http://www.springframework.org/schema/util"
	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/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/util
                           http://www.springframework.org/schema/util/spring-util.xsd">
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	<property name="locations">
    		<list>
    			<value>classpath:persistence.properties</value>
    		</list>
    	</property>
    </bean>

	<bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${driverClassName}" />
		<property name="url" value="${url}" />
		<property name="username" value="${username}" />
		<property name="password" value="${password}" />
	</bean>

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="jdbcDataSource" />
		<property name="configLocation" value="classpath:mybatis-config.xml" />
	</bean>

	<bean id="personDAO" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="com.mybatis.inter.PersonDAO" />
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
	</bean>
</beans>

  测试:

package com.mybatis.test;

import java.util.List;

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

import com.mybatis.bean.Person;
import com.mybatis.inter.PersonDAO;

public class Test1 {
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		ApplicationContext context=null;
		context=new ClassPathXmlApplicationContext("applicationContext.xml");
		PersonDAO personDAO = (PersonDAO) context.getBean("personDAO");
		System.out.println("====================test==========================");
//		Person person=new Person();
//		person.setName("aaa");
//		person.setAge(12);
//		personDAO.add(person);
		List<Person> list = personDAO.findAll();
		for(Person person:list) {
			System.err.println(person.getName()+" "+person.getAge());
		}
	}

}

 工程截图:

 

时间: 2024-07-28 14:02:53

Mybatis学习之与Spring整合的相关文章

MyBatis学习24-mybatis和spring整合

1.整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spring和mybatis整合自动完成) 持久层的mapper都需要由spring进行管理. 2.整合环境的搭建 a.加入mybatis的jar包 b.加入spring的jar包 c.mybatis和Spring的整合包mybatis-spring-1.2.3 http://mvnrepository

mybatis学习笔记(14)-spring和mybatis整合

mybatis学习笔记(14)-spring和mybatis整合 mybatis学习笔记14-spring和mybatis整合 整合思路 整合环境 sqlSessionFactory 原始dao开发和spring整合后 mapper代理开发 遇到的问题 本文主要将如何将spring和mybatis整合,只是作简单的示例,没有使用Maven构建.并展示mybatis与spring整合后如何进行原始dao开发和mapper代理开发. 整合思路 需要spring通过单例方式管理SqlSessionFa

Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)

CXF与Spring整合,分两个方面给大家介绍: 1,在传统ssh项目基础上添加Web Service 赋值CXF的jar包 在web.xml配置文件中导入CXF的核心控制器:CXFServlet 在Spring配置文件中导入CXF提供Schema,xml配置文件 在Spring配置文件中使用jaxws:endpoint元素来暴露Web Service 如果要添加拦截器,在jaxws:endpoint元素里添加 inInterceptors,outInterceptors子元素 2,远程调用We

【mybatis源码学习】与spring整合Mapper接口执行原理

一.重要的接口 org.mybatis.spring.mapper.MapperFactoryBean MapperScannerConfigurer会向spring中注册该bean,一个mapper接口注册一个 该类是生产MapperProxy对象 org.apache.ibatis.binding.MapperProxy mapper接口的代理类 org.mybatis.spring.SqlSessionInterceptor sqlSession的动态代理类 org.mybatis.spr

mybatis学习小结

MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录. jdbc源码: <span style="font-size:18px;">public static voidmain(String[]

springmvc+mybatis学习笔记(汇总)

springmvc+mybatis学习笔记(汇总) 标签 : springmvc mybaits springmvcmybatis学习笔记汇总 目录 联系作者 笔记分为两大部分:mybatis和springmvc mybatis springmvc 笔记内容主要是mybatis和springmvc的一些基本概念和使用方法,涉及概念介绍.环境搭建.编程细节.运行调试等方面. 这套笔记整体偏入门和应用,适合快速上手,对底层实现和机理并未做过多分析.我后续会研读spring源码,并把学习的收获写成博客

2月4号学习的一个SSM整合项目,第一课

**MySQL:**1.这里我们采用手写代码创建相关表,掌握这种能力对我们以后的项目二次上线会有很大的帮助:2.SQL技巧:3.事务和行级锁的理解和一些应用. **MyBatis:**1.DAO层的设计与开发.2.MyBatis的合理使用,使用Mapper动态代理的方式进行数据库的访问.3.MyBatis和Spring框架的整合:如何高效的去整合MyBatis和Spring框架. **Spring:**1.Spring IOC帮我们整合Service以及Service所有的依赖.2.声明式事务.

Mybatis学习(五)

2.搭建Java工程 A.加入mybatis核心包.依赖包.数据驱动包 B.在classpath下创建log4j.properties和db.propertites(mybatis默认使用log4j作为输出日志信息) log4j.properties # Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.Con

【SpringMVC学习04】Spring、MyBatis和SpringMVC的整合

前两篇springmvc的文章中都没有和mybatis整合,都是使用静态数据来模拟的,但是springmvc开发不可能不整合mybatis,另外mybatis和spring的整合我之前学习mybatis的时候有写过一篇,但是仅仅是整合mybatis和spring,所以这篇文章我系统的总结一下spring.mybatis和springmvc三个框架的整合(后面学习到maven时,我会再写一篇使用maven整合的文章,这篇没有用到maven). 1. jar包管理 我之前有写过一篇spring.hi