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

Spring+Mybatis J2SE小例总结

时间:2014-12-10 21:14:37      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:spring   mybatis   j2se   java   数据库   

package dao;

import java.util.List;

import model.User;

public interface UserDao {

	public User getUser(int i);
	
	public List<User> getAllUser();
	
	public int insertUser(User u);
	
	public int updateUser(User u);
	
	public int deleteUser(int i);
	
}

<?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="dao.UserDao"> 
	<select id="getUser" parameterType="int" 
		resultType="User">
		select * from t_user where id=#{id}
	</select>
	<select id="getAllUser" resultType="User">
	select * from t_user
	</select>
	<delete id="deleteUser" parameterType="int" >
	delete from t_user where id=#{id}
	</delete>
	<update id="updateUser" parameterType="User">
	update t_user set username=#{username}, password=#{password} where id=#{id}
	</update>
	<insert id="insertUser" parameterType="User">
	insert into t_user(username,password) values(#{username},#{password})
	</insert>
	
</mapper>

package model;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;



@Component
@Scope("prototype")
public class User {
	private int id;
	private String username;
	private String password;
	public User() {
		super();		
	}
	
	public User(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	

}

package service;

import java.util.List;

import model.User;

public interface IUserSrv {

	public User getUser(int i);
	
	public List<User> getAllUser();
	
	public int insertUser(User u);
	
	public int updateUser(User u);
	
	public int deleteUser(int i);
}

package service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import dao.UserDao;
import model.User;
@Component
@Transactional
public class UserSrv implements IUserSrv {

@Resource
private  UserDao userDao;	

	@Override	
	public User getUser(int i) {
		// TODO 自动生成的方法存根		
		return userDao.getUser(i);
	}
	@Override
	public List<User> getAllUser() {
		// TODO 自动生成的方法存根
		return userDao.getAllUser();
	}

	@Override
	public int insertUser(User u) {
		// TODO 自动生成的方法存根
		return userDao.insertUser(u);
	}

	@Override
	public int updateUser(User u) {
		// TODO 自动生成的方法存根
		return userDao.updateUser(u);
	}

	@Override
	public int deleteUser(int i) {
		// TODO 自动生成的方法存根
		return userDao.deleteUser(i);
	}

}

package test;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import model.User;
import service.IUserSrv;
import util.BeanHelper;

@Component
public class Test {
	@Resource
	private User user;

	public static void main(String[] args) {
		IUserSrv userSrv = (IUserSrv) BeanHelper.getBean("userSrv");
		System.out.println(userSrv.getUser(1).getUsername());
		Test t = (Test) BeanHelper.getBean("test");
		t.user.setUsername("dd");
		t.user.setPassword("xxx");
		System.out.println(userSrv.insertUser(t.user));
	}

}

package util;

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



public class BeanHelper {
	private BeanHelper(){}
	private static ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); 		
	public static Object getBean(String beanName){
	return ac.getBean(beanName);		
	}
}

<?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: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-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">	
	
	<!-- 1. 数据源 : DriverManagerDataSource -->
	<context:annotation-config></context:annotation-config>
	<bean id="datasource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=UTF-8" />
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>
	
	<!-- 2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource / typeAliasesPackage -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="datasource" />
		<property name="typeAliasesPackage" value="model" />
	</bean>
	<!-- 3. mybatis自动扫描加载Sql映射文件 : MapperScannerConfigurer sqlSessionFactory 
		/ basePackage -->
	<bean id="config" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="dao" />
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
	</bean>

	<!-- 4. 事务管理 : DataSourceTransactionManager -->
	<bean id="manager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="datasource" />
	</bean>

	<!-- 5. 使用声明式事务 -->
	<tx:annotation-driven transaction-manager="manager" />		
	<!-- 6. 扫描加载各包下的Bean -->
	<context:component-scan base-package="service,model,test"></context:component-scan>
</beans>



Spring+Mybatis J2SE小例总结

标签:spring   mybatis   j2se   java   数据库   

原文地址:http://blog.csdn.net/shaobotao/article/details/41852167

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