gradle + spring4 + springmvc4 + mybatis3 + driud1 + logback1(CRUD操作)

阅读更多

 

 依赖库:

gradle + spring4 + springmvc4 + mybatis3 + driud1 + logback1

 

目录结构:



gradle + spring4 + springmvc4 + mybatis3 + driud1 + logback1(CRUD操作)_第1张图片
 

 

 

build.gradle

 

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
apply plugin: 'jetty'

// JDK 6
sourceCompatibility = 1.6
targetCompatibility = 1.6

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
 
	compile 'ch.qos.logback:logback-classic:1.1.3'
	compile 'org.springframework:spring-webmvc:4.1.6.RELEASE'
	compile 'org.springframework:spring-tx:4.1.6.RELEASE'
	compile 'org.springframework:spring-jdbc:4.1.6.RELEASE'
	compile 'org.springframework:spring-test:4.1.6.RELEASE'
	compile 'javax.servlet:jstl:1.2'
	compile 'com.alibaba:druid:1.0.2'
	compile 'mysql:mysql-connector-java:5.1.25'
	compile 'org.mybatis:mybatis:3.2.2'
	compile 'org.mybatis:mybatis-spring:1.2.0'
	compile 'org.aspectj:aspectjweaver:1.7.2'
	compile 'org.freemarker:freemarker-gae:2.3.23'
	

}

// Embeded Jetty for testing
jettyRun{
	contextPath = "spring4"
	httpPort = 8080
}

jettyRunWar{
	contextPath = "spring4"
	httpPort = 8080
}

//For Eclipse IDE only
eclipse {

  wtp {
    component {
      
      //define context path, default to project folder name
      contextPath = 'spring4'
      
    }
    
  }
}

 

spring-core-config.xml


 


 
 	
	

	
	
	
	
	
	
		
		
		

		
		
		
		
		
		
		
		
		
		
		
		

		
		
		
		

		
		
		
		

		
		
		
		
		
		

		
		
		
	
	
	
	
		
		
		
		
	

	

	
	
		
	

	
		
			
			
			
			
			
			
			
			
		
	
	
	
		
		
	
	
	

 spring-mvc-config.xml

 


 
	
 
	
		
		
		
	
 
	
	 
	
 
	

 

web.xml


 



	BusinessOnline
	Spring MVC web application

	
	
		springmvc-dispatcher
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			/WEB-INF/spring-mvc-config.xml
		
		1
	

	
		springmvc-dispatcher
		*.action
	

	
	
		org.springframework.web.context.ContextLoaderListener
	

	
		contextConfigLocation
		/WEB-INF/spring-core-config.xml
	

	
	
		SpringEncoding
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			utf-8
		
	
	
		SpringEncoding
		/*
	

 

logback.xml


 




	
		

			
				%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
			

		
	

	
		
	
	
	
		
	
	
	
		
	


 

jdbc.properties

 

 

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

#jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
#jdbc.username=scott
#jdbc.password=tiger


 

 

sqlMapConfig.xm

 

 




	
	

 

jar:



 

实体类:

package com.business.domain;

public class Student {

	private int id;
	private String username;
	private String 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;
	}
	
	
}

 

dao

BaseDao:

 
package com.business.dao;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


/**
 * 泛型类  基础的dao接口
 * @author xiaoxuan
 *
 * @param 
 */
public interface BaseDao {

//	public List findPage(Page page);				//分页查询
	public List find(Map paraMap);				//带条件查询,条件可以为null,既没有条件;返回list对象集合
	public T get(Serializable id);					//只查询一个,常用于修改
	public void insert(T entity);					//插入,用实体作为参数
	public void update(T entity);					//修改,用实体作为参数
	public void deleteById(Serializable id);		//按id删除,删除一条;支持整数型和字符串类型ID
	public void delete(Serializable[] ids);			//批量删除;支持整数型和字符串类型ID
	
}

 

 

StudentDao:

package com.business.dao;

import com.business.domain.Student;

public interface StudentDao extends BaseDao {

}

 

dao实现

BaseDaoImpl:
package com.business.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

import com.business.dao.BaseDao;


public abstract class BaseDaoImpl extends SqlSessionDaoSupport implements BaseDao{
	@Autowired
	//mybatis-spring 1.0无需此方法;mybatis-spring1.2必须注入。
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
		super.setSqlSessionFactory(sqlSessionFactory);
	}
	
	private String ns; //命名空间
	public String getNs() {
		return ns;
	}
	public void setNs(String ns) {
		this.ns = ns;
	}
	
	public List find(Map paraMap) {
		List oList = this.getSqlSession().selectList(ns + ".find",paraMap);
		return oList;
	} 
	
	public T get(Serializable id) {
		return this.getSqlSession().selectOne(ns + ".get", id);
	}
	
	public void insert(T entity) {
		this.getSqlSession().insert(ns + ".insert", entity);
	}
	
	public void update(T entity) {
		this.getSqlSession().update(ns + ".update", entity);
	}
	
	public void deleteById(Serializable id) {
		this.getSqlSession().delete(ns + ".deleteById", id);
	}

	public void delete(Serializable[] ids) {
		this.getSqlSession().delete(ns + ".delete", ids);
	}
	

}
 StudentDaoImpl:
package com.business.dao.impl;

import org.springframework.stereotype.Repository;

import com.business.dao.StudentDao;
import com.business.domain.Student;

@Repository
public class StudentDaoImpl extends BaseDaoImpl implements StudentDao {

	public StudentDaoImpl(){
		super.setNs("com.business.mapper.StudentMapper");
	}

}
 mapper
StudentMapper.xml




	
		
		
		
		
	

	
	
	
	
	
		update student   
		set password=#{password}
		where username=#{username}
	
	
	
		insert into student
		(username,password)
		values(
			#{username},
			#{password}
			
		)
	
	
	  
        delete student where id=#{id}  
    
 
StudentController:
package com.business.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.business.dao.StudentDao;
import com.business.domain.Student;

@Controller
public class StudentController {

	@Autowired
	StudentDao studentDao;
	
	@RequestMapping("/test.action")
	public String testget(Model model){
		Student stu = studentDao.get(1);
		System.err.println(stu);
		model.addAttribute("stu", stu);
		return "/index";
	}
}
 
 

 

 

 

 

  • gradle + spring4 + springmvc4 + mybatis3 + driud1 + logback1(CRUD操作)_第2张图片
  • 大小: 24.7 KB
  • 查看图片附件

你可能感兴趣的:(gradle,spring4,springmvc4,mybatis3,druid)