利用Intellij+MAVEN完成Spring整合Mybatis项目详解

 利用Intellij+MAVEN完成Spring整合Mybatis项目详解

1 创建MAVEN项目,配置pom.xml

  4.0.0
  SpringMybatisTest
  SpringMybatisTest
  war
  1.0-SNAPSHOT
  SpringMybatisTest Maven Webapp
  http://maven.apache.org

  

    
      junit
      junit
      3.8.1
      test
    

    
      org.springframework
      spring-test
      4.0.6.RELEASE
      test
    

    
    
      org.springframework
      spring-beans
      4.3.1.RELEASE
    

    
      org.springframework
      spring-core
      4.3.1.RELEASE
    

    
      org.springframework
      spring-context
      4.3.1.RELEASE
    


    
    
      org.springframework
      spring-web
      4.3.1.RELEASE
    
    
      org.springframework
      spring-webmvc
      4.3.1.RELEASE
    

    
    
    
      javax.servlet
      jstl
      1.2
    

    
      taglibs
      standard
      1.1.2
    

    
    
      javax.servlet
      servlet-api
      2.5
    

    
    
      javax.servlet.jsp
      jsp-api
      2.2
    

    
    
      mysql
      mysql-connector-java
      5.1.6
    

    
    
      org.springframework
      spring-jdbc
      3.0.5.RELEASE
    

    
    
      com.mchange
      c3p0
      0.9.5.1
    

    
    
      org.aspectj
      aspectjweaver
      1.8.6
    

    
      org.aspectj
      aspectjrt
      1.8.6
    

    
    
      org.mybatis
      mybatis
      3.4.1
    

    
    
    
      org.mybatis
      mybatis-spring
      1.3.0
    


    
      junit
      junit
      RELEASE
    
    
      SpringMybatisTest
      SpringMybatisTest
      1.0-SNAPSHOT
    
    
      org.springframework
      spring-test
      RELEASE
    
  

  
    SpringMybatisTest

    
      
      
        org.eclipse.jetty
        jetty-maven-plugin
        9.3.10.v20160621
      

      
      
        org.mybatis.generator
        mybatis-generator-maven-plugin
        1.3.2
        
          true
          true
        
      

      
        org.apache.maven.plugins
        maven-compiler-plugin
        
          1.7
          1.7
        
      
    
  



2 建java包后建实体类:
实体类Employee.java
package com.springmybatis.model;

/**
 * Created by wanggenshen_sx on 2016/12/23.
 */
public class Employee {

	private int id;
	private String lastName;
	private String email;

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", lastName=" + lastName + ", email="
				+ email+" ]" ;
	}
}

EmployeeMapper.java,采用的注解的方式,即@Select
package com.springmybatis.service;

import com.springmybatis.model.Employee;
import org.apache.ibatis.annotations.Select;

import java.util.List;


/**
 * Created by wanggenshen_sx on 2016/12/26.
 */
public interface EmployeeMapper {

	@Select("select id,lastName,email from employee where id=#{id}")
	Employee findById(int id);

	@Select("select id,lastName,email from employee")
	List findAll();
}

   测试类:SpringMybatisTest1.java
package com.springmybatis.test;

import com.springmybatis.model.Employee;
import com.springmybatis.service.EmployeeMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.util.List;

/**
 * Created by wanggenshen_sx on 2016/12/26.
 */
@RunWith(SpringJUnit4ClassRunner.class)//使用Springtest框架
@ContextConfiguration(locations={"classpath:spring.xml"})//加载配置
public class SpringMybatisTest1 {
	@Autowired
	private EmployeeMapper employeeMapper;

	@Test
	public void getUser(){
		Employee user = employeeMapper.findById(1);
		System.out.println(user);
	}

	@Test
	public void findAll(){
		List users = employeeMapper.findAll();
		System.out.println(users);
	}
	@Test
	public void test(){
		System.out.println(12);
	}


}


3 编写spring配置文件,整合mybatis.注意这里使用的事EmployeeMapper接口,采用的是注解的方式,所以要在配置文件中去注册这个接口。(如果用配置文件的方式就是要注册对应接口的xml文件)


id="config" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    name="basePackage" value="com.springmybatis.service"/>
    name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>




    
    
        
        
        
        
    

    
    
        
        
    

    
    
        
        
    

    
    
        
    

    
    



项目结构:

利用Intellij+MAVEN完成Spring整合Mybatis项目详解_第1张图片








你可能感兴趣的:(SSM)