基于Java配置整合spring+mybatis

 基于java配置整合Spring+MyBatis。

 

1.创建简单的数据库及数据表。


2.创建一个简单的maven工程。(http://blog.csdn.net/zhshulin/article/details/37921705)

 
基于Java配置整合spring+mybatis_第1张图片


3.配置pom.xml文件,加载依赖的jar包

 


	4.0.0
	com.java.spring
	SMybatis02
	war
	0.0.1-SNAPSHOT
	SMybatis02 Maven Webapp
	http://maven.apache.org

	
		
		4.0.2.RELEASE
		
		3.2.6
		
		1.7.7
		1.2.17
	

	
		
			junit
			junit
			3.8.1
			test
		

		
			org.springframework
			spring-core
			${spring.version}
		

		
			org.springframework
			spring-web
			${spring.version}
		
		
			org.springframework
			spring-oxm
			${spring.version}
		
		
			org.springframework
			spring-tx
			${spring.version}
		

		
			org.springframework
			spring-jdbc
			${spring.version}
		

		
			org.springframework
			spring-webmvc
			${spring.version}
		
		
			org.springframework
			spring-aop
			${spring.version}
		

		
			org.springframework
			spring-context-support
			${spring.version}
		

		
			org.springframework
			spring-test
			${spring.version}
		
		
		
			org.mybatis
			mybatis
			${mybatis.version}
		
		
		
			org.mybatis
			mybatis-spring
			1.2.2
		
		
		
			javax
			javaee-api
			7.0
		
		
		
			mysql
			mysql-connector-java
			5.1.30
		
		
		
			commons-dbcp
			commons-dbcp
			1.2.2
		
		
		
			jstl
			jstl
			1.2
		
		
		
		
			log4j
			log4j
			${log4j.version}
		


		
		
			com.alibaba
			fastjson
			1.1.41
		


		
			org.slf4j
			slf4j-api
			${slf4j.version}
		

		
			org.slf4j
			slf4j-log4j12
			${slf4j.version}
		
		
		
		
			org.codehaus.jackson
			jackson-mapper-asl
			1.9.13
		
		
		
			commons-fileupload
			commons-fileupload
			1.3.1
		
		
			commons-io
			commons-io
			2.4
		
		
			commons-codec
			commons-codec
			1.9
		
		
		
        com.alibaba
        druid
        1.0.16
    
	
	
		SMybatis02
	

 

 

4.创建数据库中对应的实体类

 

package com.test.domain;

public class User {

	private int id;
	private String name;
	private int age;

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

	public int getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}

}

 

 

5.创建mybatis的映射文件

 





	
		
		
		
	

	

	
		INSERT INTO user (id,name,age) VALUES (#{id},#{name},#{age})
	

	
		UPDATE user SET name=#{name},age=#{name} WHERE id=#{id}
	

	
		DELETE FROM user WHERE id=#{id}
	

 

 

6.创建UserMapper.java及其它业务层方法

 

package com.test.mapper;

import org.springframework.stereotype.Repository;

import com.test.domain.User;

@Repository
public interface UserMapper {
	
	public User getUser(String name);
	public void insertUser(User user);
	public void deleteUser(int id);
	public void updateUser(User user);
	
}

 

package com.test.api;

import com.test.domain.User;

public interface UserService {
	
	public User getUser(String name);
	
}

 

package com.test.provider;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.test.domain.User;
import com.test.mapper.UserMapper;

@Service
public class UserServiceImpl implements com.test.api.UserService {
	
	@Autowired
	UserMapper userMapper;
	
	public User getUser(String name) {
		return userMapper.getUser(name);
	}

}

 

7.创建属性文件,并创建配置类整合spring+mybatis

 
基于Java配置整合spring+mybatis_第2张图片

 

package com.test.config;

import java.beans.PropertyVetoException;
import java.io.IOException;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@PropertySource("classpath:/jdbc.properties")
@MapperScan(basePackages="com.test.mapper")
@ComponentScan("com.test.provider")
public class Config {

	@Autowired
	Environment env;

	@Bean
	public DataSource dataSource() throws PropertyVetoException {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(env.getProperty("jdbc.driver"));
		dataSource.setUrl(env.getProperty("jdbc.url"));
		dataSource.setUsername(env.getProperty("jdbc.username"));
		dataSource.setPassword(env.getProperty("jdbc.password"));
		return dataSource;

	}

	@Bean
	public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws IOException {
		ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSource);
		sqlSessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources("classpath*:/*Mapper.xml"));
		return sqlSessionFactoryBean;
	}

}

  

8.创建测试方法

package com.main.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.test.api.UserService;
import com.test.domain.User;
import com.test.provider.UserServiceImpl;

public class MainTest {
	
	private static UserService userService;
	
	@BeforeClass
	public static void init(){
		ApplicationContext cx = new AnnotationConfigApplicationContext("com.test.config");
		userService = (UserService)cx.getBean(UserServiceImpl.class);
	}
	
	@Test
	public void getUserByName(){
		
		User user = userService.getUser("jim");
		System.out.println(user);
	}
}

 

 

Druid提供了MySql、Oracle、Postgresql、SQL-92的SQL的完整支持,这是一个手写的高性能SQL Parser,支持Visitor模式,使得分析SQL的抽象语法树很方便。
简单SQL语句用时10微秒以内,复杂SQL用时30微秒。
通过Druid提供的SQL Parser可以在JDBC层拦截SQL做相应处理,比如说分库分表、审计等。Druid防御SQL注入攻击的WallFilter,就是通过Druid的SQL Parser分析语义实现的

 

一个菜鸟的的学习笔记,如果有路过的朋友看到,多批评指正

 

 

你可能感兴趣的:(spring,mybatis)