码迷,mamicode.com
首页 > 编程语言 > 详细

Mybatis学习之与Spring整合

时间:2015-09-29 14:35:50      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

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());
		}
	}

}

 工程截图:

 技术分享

Mybatis学习之与Spring整合

标签:

原文地址:http://www.cnblogs.com/zcs201093189/p/4846185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!