javaee之Spring的整合及使用技巧

使用Spring來對JDBC進行整合
applicationContext.xml:





	
	
		
		
		
		
	
	
	
	
		
	
	
	
		
	
	

User:

public class User {


	private int id;
	private String name;
	private String gender;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", gender=" + gender + "]";
	}
	
	
	
}

UserDao:

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;


public class UserDao {


	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	//添加的方法
	public void save(User user){
		jdbcTemplate.update("insert into t_user(id,name,gender) values (?,?,?)",user.getId(),user.getName(),user.getGender() );
	}
	
	//修改的方法
	public void update(User user){
		jdbcTemplate.update("update t_user set name=?,gender=? where id=?",user.getName(),user.getGender(),user.getId());
	}
	
	//刪除的方法
	public void delete(int id){
		jdbcTemplate.update("delete from t_user where id=?",id);
	}
	
	//查詢 所有的方法
	public List findAll(){
		return jdbcTemplate.query("select * from t_user", new RowMapper(){


			@Override
			public Object mapRow(ResultSet rs, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				User u = new User();
				u.setId(rs.getInt("id"));
				u.setName(rs.getString("name"));
				u.setGender(rs.getString("gender"));
				return u;
			}
			
		});
	}
	
	//查詢一個用戶
	public User findone(int id){
		 return jdbcTemplate.queryForObject("select * from t_user where id=?", new RowMapper(){


			@Override
			public Object mapRow(ResultSet rs, int arg1) throws SQLException {
				User u = new User();
				u.setId(rs.getInt("id"));
				u.setName(rs.getString("name"));
				u.setGender(rs.getString("gender"));
				return u;
				
			}
			
		},id);
	}
	
	//分頁查詢
	public List findByPage(int curPage,int pageSize){
		 return jdbcTemplate.query("select * from t_user limit ?,?", new RowMapper(){


				@Override
				public Object mapRow(ResultSet rs, int arg1) throws SQLException {
					User u = new User();
					u.setId(rs.getInt("id"));
					u.setName(rs.getString("name"));
					u.setGender(rs.getString("gender"));
					return u;
					
				}
				
			},curPage,pageSize);
		}
	
	//查詢總記錄數
	public int findCount(){
		return jdbcTemplate.queryForObject("select count(*) from t_user", Long.class).intValue();
	}
}

測試類:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class text {


	@Test
	public void test01(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserDao userdao = (UserDao)ac.getBean("userdao");
		User u = new User();
		u.setId(3);
		u.setName("王五");
		u.setGender("男");
		userdao.save(u);
		
	}
	
	@Test
	public void test02(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserDao userdao = (UserDao)ac.getBean("userdao");
		User u = new User();
		u.setId(2);
		u.setName("李四");
		u.setGender("女");
		userdao.update(u);
		
	}
	
	@Test
	public void test03(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserDao userdao = (UserDao)ac.getBean("userdao");
		userdao.delete(2);
		
	}
	
	@Test
	public void test04(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserDao userdao = (UserDao)ac.getBean("userdao");
		System.out.println(userdao.findAll());
		
	}
	
	@Test
	public void test05(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserDao userdao = (UserDao)ac.getBean("userdao");
		System.out.println(userdao.findone(2));
		
	}
	
	@Test
	public void test06(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserDao userdao = (UserDao)ac.getBean("userdao");
		System.out.println(userdao.findByPage(1, 2));
	}
	
	@Test
	public void test07(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserDao userdao = (UserDao)ac.getBean("userdao");
		System.out.println(userdao.findCount());
	}
}

Spring整合Hibernate,使用HibernateTemplate工具來進行一些功能

applicationContext.xml:





	
	
		
		
		
		
	
	
	
	
	
	
	
	
		
		
			
				update
				true
			
		
		
			
				demo2_hibernate/Student.hbm.xml
			
		
	
	
	
	
		
	
	
	
		
	

Student:

public class Student {


	private int id;
	private String name;
	private String gender;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", gender=" + gender
				+ "]";
	}
	
	
	
}

Student.hbm.xml:








	
	
		
			
		
		
		
		
	

StudentDao:

import java.util.List;


import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.HibernateTemplate;


public class StudentDao {


	private HibernateTemplate hibernateTemplate;
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}


	public List findAll(){
		return hibernateTemplate.loadAll(Student.class);
		//return (List) hibernateTemplate.find("from Student");
	}
	
	public Student findById(int id){
		return hibernateTemplate.get(Student.class, id);
	}
	
	public List findByPage(final int curpage,final int pagesize){
		return hibernateTemplate.execute(new HibernateCallback<list>() {


			@Override
			public List doInHibernate(Session session) throws HibernateException {
				// TODO Auto-generated method stub
				Query query = session.createQuery("from Student");
				query.setFirstResult(curpage);
				query.setMaxResults(pagesize);
				return query.list();
			}
		});
	}
	
	public int findCount(){
		return hibernateTemplate.execute(new HibernateCallback() {


			@Override
			public Long doInHibernate(Session session) throws HibernateException {


				Query query = session.createQuery("select count(*) from Student");	
				
				return (Long) query.uniqueResult();
			}
		}).intValue();
	}
	
	//添加操作需要事務管理並且還需要aop
	public void save(Student student){
		hibernateTemplate.save(student);
	}
}
</list

測試類:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;




public class Text {


	@Test
	public void test01(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		StudentDao dao = (StudentDao) ac.getBean("studentdao");
		System.out.println(dao.findAll());
	}
	
	@Test
	public void test02(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		StudentDao dao = (StudentDao) ac.getBean("studentdao");
		System.out.println(dao.findById(2));
	}
	
	@Test
	public void test03(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		StudentDao dao = (StudentDao) ac.getBean("studentdao");
		System.out.println(dao.findByPage(1, 2));
		
	}
	
	@Test
	public void test04(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		StudentDao dao = (StudentDao) ac.getBean("studentdao");
		System.out.println(dao.findCount());
	}
	
	@Test
	public void test05(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		StudentDao dao = (StudentDao) ac.getBean("studentdao");
		Student student = new Student();
		student.setName("張三");
		student.setGender("男");
		dao.save(student);
	}
}

使用Spring來整合Struts2,除瞭在applicationContext.xml要配置action外,還需要在web.xml的文件中定義Spring的核心監聽器
applicationContext.xml:



	
	
	
	
	
	
	
		
		
	
	

struts.xml:








	
		
		
			/succ.jsp
		
	


User:

public class User {
	private String name;
	private String gender;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", gender=" + gender + ", age=" + age
				+ "]";
	}
	
}

UserService:

public class UserServiceImpl implements IUserService {


	public void save(User user) {
		System.out.println("進入業務方法:保存用戶:"+user);
	}


}

UserAction:

import com.opensymphony.xwork2.ActionSupport;


public class UserAction extends ActionSupport{
	//接收一個業務邏輯對象
	private IUserService userService;
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}




	//接收參數
	private User user;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}




	//註冊方法
	public String reg(){
		System.out.println("進入瞭註冊方法");
		System.out.println(user);
		
		//調用業務邏輯
		userService.save(user);
		
		return SUCCESS;
	}
}

web.xml:


		org.springframework.web.context.ContextLoaderListener
	
	
		contextConfigLocaltion
		classpath:applicationContext.xml
	

學習完Spring來用整合Struts2和hibernate,那麼以後就可以通過三大框架來進行開發項目瞭

發佈留言