Maven + Spring + SpringMVC + Spring JPA <2>

上一篇全是整的配置,这篇整点代码测试一下,创建下图中红框框住的文件,其中HelloWorldController.java和helloWorld.jsp是测试SpringMVC的,其他则是测试SpringJpa的。

Maven + Spring + SpringMVC + Spring JPA <2>_第1张图片

HelloWorldController.java

package org.mice.test;

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

@Controller
public class HelloWorldController {

    @RequestMapping("/helloWorld")
    public String helloWorld(Model model) {
    	
        model.addAttribute("message", "Hello World!");
        return "helloWorld";
    }
}

Student.java

package org.mice.test;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "STUDENT")
public class Student {
	@Id
	@GeneratedValue
	private Integer id;
	// 账号
	private String account;
	// 姓名
	private String name;
	// 性别
	private String sex;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
}
StudentRepository.java

package org.mice.test;

import org.springframework.data.repository.CrudRepository;

public interface StudentRepository extends CrudRepository<Student, Integer>{

}

StudentService.java

package org.mice.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("studentService")
public class StudentService {
	@Autowired
    private StudentRepository studentRepository;//注入UserRepository

    @Transactional
    public void saveStudent(Student student) {
    	studentRepository.save(student);
        
    }

    @Transactional(readOnly=true)
    public Student findStudentById(Integer id) {
        return studentRepository.findOne(id);
    }

    @Transactional
    public void updateStudent(Student student) {
    	studentRepository.save(student);
    }

    @Transactional
    public void deleteStudentById(Integer id) {
    	studentRepository.delete(id);
    }
}
Test.java

package org.mice.test;

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

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		StudentService studentService = (StudentService)Test.getBean("studentService");
		
		Student student = new Student();
		student.setName("chenph");
		student.setAccount("yueritian");
		student.setSex("1");
		studentService.saveStudent(student);
	}

	// 加载配置文件
	private static ApplicationContext _ctx = new ClassPathXmlApplicationContext(
			"config/applicationContext.xml");

	/**
	 * 获取bean对象
	 * 
	 * @param name
	 *            bean名称
	 * @return
	 */
	public static final Object getBean(String name) {
		return getContext().getBean(name);
	}

	public static ApplicationContext getContext() {
		return _ctx;
	}
}

helloWorld.jsp

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'helloWorld.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    This is my JSP page. ${message} <br>
  </body>
</html>

测试mvc:http://localhost:port/projectname/helloWorld.do

测试jpa:直接Run Test,查看数据库记录,运行这个的过程中会报“java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Lja”,网上资料说的很清楚是javaee包与hibernate-jpa的包冲突了,jpa这个jar里是包括上边提到的这个方法的,所以,对缺少这个方法的jar包做删除处理就可以了。

明后天有空就开始来看看mvc和jpa是如何用到实际需求中,比如json啦,多对多,一对多关系啦。


你可能感兴趣的:(Maven + Spring + SpringMVC + Spring JPA <2>)