maven+springMVC+myBatis+mysql+junit+slf4j+logback示例代码总结

1. 创建Maven Project



2.创建文件夹和相应的文件,总体目录结构如下


3.编写配置文件们

pom.xml内容如下:这个配置文件是用来引入项目所需要的jar包的


  4.0.0
  cn.edu.nankai
  demo
  war
  0.0.1-SNAPSHOT
  demo Maven Webapp
  http://maven.apache.org
  
		4.0.2.RELEASE
		3.2.6
		1.6.4
		1.0.1
	

	
		
			junit
			junit
			4.11
			
			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
		
		
		
		
		
			com.alibaba
			fastjson
			1.1.41
		

		
		
			org.slf4j
			slf4j-api
			${slf4j.version}
		
		
		
			ch.qos.logback
			logback-core
			${logback.version}
		
		
		
			ch.qos.logback
			logback-access
			${logback.version}
		
		
		
			ch.qos.logback
			logback-classic
			${logback.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
		

		
			commons-lang
			commons-lang
			2.6
		

		
			org.mybatis.generator
			mybatis-generator-core
			1.3.2
		

		
			org.json
			json
			20140107
		

	
  
    demo
  


config.properties内容如下:这个配置文件是用来配置数据库连接信息的

river=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
username=root
password=root
initialSize=0
maxActive=20
maxIdle=20
minIdle=1
maxWait=60000


logback.xml内容如下:这个配置文件是用来配置日志的处理方式的



	
		

	
	
	
	
	
	
	
		
			%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{20} - %msg%n
		
	
	
	
	
		
          ERROR
          ACCEPT
          DENY
        
		${log_dir}/example_error.log
		
			${log_dir}/example_error.%d.log
			${maxHistory}
		
		
			%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{20} - %msg%n
			UTF-8
		
	
	
	
	
	
		
          INFO
        
		${log_dir}/example_error.log
		
			${log_dir}/example_error.%d.log
			${maxHistory}
		
		
			%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{20} - %msg%n
			UTF-8
		
	
		
	
	  
    	 
     	
	
     
	


spring-mvc.xml内容如下:这个文件是用来配置spring mvc的control所在的包和其他spring mvc相关信息



    
    
    
    
        
            
                text/html;charset=UTF-8
            
        
    
    
    
        
            
                 
            
        
    
    
    
        
        
    

    
    
        
        
        
        
        
        
    



spring-mybatis.xml内容如下:这个配置文件用于springmvc和mybatis的整合




    
    
    
    
        
    

    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
    

    
    
        
        
    

    
    
        
    


web.xml:这个配置文件用于配置项目的全局信息





  Archetype Created Web Application
  
   
    
        contextConfigLocation
        classpath:spring-mybatis.xml
    
    
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        true
        
            encoding
            UTF-8
        
    
    
        encodingFilter
        /*
    
    
    
    

    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        org.springframework.web.util.IntrospectorCleanupListener
    
    
    
    
        SpringMVC
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
        
        1
        
        true
    
    
        SpringMVC
        
        
        *.htm
    
    
    
    	/WEB-INF/jsp/login.jsp
    
    
	


4.根据数据库生成model,mapper文件和相应的dao层

数据库信息如下:


参考我的这篇博客: mybatis如何自动生成实体类,映射文件以及对应的dao层 ,生成后将文件复制到相应的包下,目录如下


 TestUser.java:

package cn.edu.nankai.demo.model;

public class TestUser {
    private Integer uid;

    private String uname;

    private String udesc;

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname == null ? null : uname.trim();
    }

    public String getUdesc() {
        return udesc;
    }

    public void setUdesc(String udesc) {
        this.udesc = udesc == null ? null : udesc.trim();
    }
}


 
  
TestUserMapper.java:

package cn.edu.nankai.demo.dao;

import cn.edu.nankai.demo.model.TestUser;

public interface TestUserMapper {
    int deleteByPrimaryKey(Integer uid);

    int insert(TestUser record);

    int insertSelective(TestUser record);

    TestUser selectByPrimaryKey(Integer uid);

    int updateByPrimaryKeySelective(TestUser record);

    int updateByPrimaryKey(TestUser record);
}

TestUserMapper.xml:




  
    
    
    
  
  
    uid, uname, udesc
  
  
  
    delete from test_user
    where uid = #{uid,jdbcType=INTEGER}
  
  
    insert into test_user (uid, uname, udesc
      )
    values (#{uid,jdbcType=INTEGER}, #{uname,jdbcType=VARCHAR}, #{udesc,jdbcType=VARCHAR}
      )
  
  
    insert into test_user
    
      
        uid,
      
      
        uname,
      
      
        udesc,
      
    
    
      
        #{uid,jdbcType=INTEGER},
      
      
        #{uname,jdbcType=VARCHAR},
      
      
        #{udesc,jdbcType=VARCHAR},
      
    
  
  
    update test_user
    
      
        uname = #{uname,jdbcType=VARCHAR},
      
      
        udesc = #{udesc,jdbcType=VARCHAR},
      
    
    where uid = #{uid,jdbcType=INTEGER}
  
  
    update test_user
    set uname = #{uname,jdbcType=VARCHAR},
      udesc = #{udesc,jdbcType=VARCHAR}
    where uid = #{uid,jdbcType=INTEGER}
  


5.编写service和serviceImpl层

TestUserService.java:

package cn.edu.nankai.demo.service;

import cn.edu.nankai.demo.model.TestUser;

public interface TestUserService {
	
	int insert(TestUser record);
	
}


TestUserServiceImpl.java:

package cn.edu.nankai.demo.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.edu.nankai.demo.dao.TestUserMapper;
import cn.edu.nankai.demo.model.TestUser;
import cn.edu.nankai.demo.service.TestUserService;

@Service("testUserService")
public class TestUserServiceImpl implements TestUserService {

	@Resource
	TestUserMapper testUserMapper;
	public int insert(TestUser record) {
		// TODO Auto-generated method stub
		return testUserMapper.insert(record);
	}

}

6.编写页面login.htm:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'login.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
    This is my JSP page. 
点我增加用户

7.编写Controller

  

8.运行结果









 











你可能感兴趣的:(java)