Spring-mybatis整合Springmvc

Spring-mybatis整合

1.导入spring,mybatis jar包


        
            org.springframework
            spring-context
            5.0.2.RELEASE
        

        
            org.springframework
            spring-jdbc
            5.0.2.RELEASE
        

        
            org.springframework
            spring-tx
            5.0.2.RELEASE
        
        
            junit
            junit
            4.12
        
        
            mysql
            mysql-connector-java
            5.1.6
        
        
            org.springframework
            spring-test
            5.0.2.RELEASE
        
        
            org.aspectj
            aspectjweaver
            1.8.7
        

        
        
            commons-dbutils
            commons-dbutils
            1.4
        

        
            mysql
            mysql-connector-java
            5.1.6
        

        
            c3p0
            c3p0
            0.9.1.2
        
        
            org.mybatis
            mybatis
            3.3.0
        
        
        
            org.mybatis
            mybatis-spring
            1.2.3
        
    

2.spring-mybatis的xml配置文件



	
    

    
    
        
        
        
        
        
    

    
    
    
    

    
        
        
    

    
    
        
        
    
    

3.Dao层接口

public interface UserDao {
    User selectById(Integer id);
}

4.Service层

4.1Service接口

public interface UserService {
    public User findById(Integer id);
}

4.2Service实现类

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;

    public User findById(Integer id){
        return userDao.selectById(id);
    }
}

5.实体类

public class User {
    private String id;
    private String username;
    private Date birthday;
    private String address;

    public String getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", birthday=" + birthday +
                ", address='" + address + '\'' +
                '}';
    }
}

6.mybatis的sql Mapper文件



        
    

7.测试调用

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class testSpringMybatis {

    @Autowired
    private UserServiceImpl userService;
    @Test
    public void test01(){
        User byId = userService.findById(1);
        System.out.println(byId);
    }
}

Spring+mybatis整合Springmvc

POM.XML



    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.4

    
    
      com.fasterxml.jackson.core
      jackson-annotations
      2.9.4
    

    
    com.fasterxml.jackson.core
    jackson-core
    2.9.4
    

 
      org.springframework
      spring-context
      5.0.2.RELEASE
    
    
      org.springframework
      spring-context-support
      5.0.2.RELEASE
    
    
      org.springframework
      spring-web
      5.0.2.RELEASE
    
 
    
      org.springframework
      spring-webmvc
      5.0.2.RELEASE
    
    
    
      org.springframework
      spring-jdbc
      5.0.2.RELEASE
    

    
    
      org.springframework
      spring-test
      5.0.2.RELEASE
    
    
      org.springframework
      spring-aspects
      5.0.2.RELEASE
    
    
      org.springframework
      spring-expression
      5.0.2.RELEASE
    
    
      org.springframework
      spring-orm
      5.0.2.RELEASE
    
    
      org.springframework
      spring-aop
      5.0.2.RELEASE
    

  
    org.aspectj
    aspectjweaver
    1.8.5
  
  
  
    javax.servlet
    jstl
    1.2
  
  
  
  
      org.mybatis
      mybatis
      3.5.0
  
  
    mysql
    mysql-connector-java
    5.1.6
  
  
  
    commons-beanutils
    commons-beanutils
    1.8.0
  
  
    log4j
    log4j
    1.2.12
  
 
     org.mybatis
     mybatis-spring
     2.0.0
     
 
     com.mchange
     c3p0
     0.9.5.2
 
  
    org.mybatis
    mybatis
    3.5.0
  

  
    commons-fileupload
    commons-fileupload
    1.3.1
  

  
  
    commons-io
    commons-io
    2.4
  
  
   
    
      javax.servlet
      servlet-api
      2.5
    


web.xml



  Archetype Created Web Application
  
    contextConfigLocation
    classpath:applicationContext.xml
  

  
    /login.html
  
  
  
    
      org.springframework.web.context.ContextLoaderListener
    
  
  
    spring
    org.springframework.web.servlet.DispatcherServlet

    


    
      contextConfigLocation
      classpath*:springmvc-servlet.xml
    

    1
  

  
  
    spring
    / 
  
  
    characterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
    
      forceEncoding
      true
    
  

  
    characterEncodingFilter
    /*
  



springmvc-servlet.xml配置






    
    
    
    
    
    
    
    
        
            
            
    
    
        
        
        
        
        
        
        
        
    

applicationContext.xml配置



    
    

    
    
        
        
        
        
        
    

    
    
    
    

    
        
        
    

    
    
        
        
    



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