用这个大部分和上一篇文章一样
下面是改动的地方
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<!-- 表示可以发布web程序 自动启动一个tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 集成hibernate -->
<!-- 操作数据源 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 添加转译jsp的jar -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- 对象关系映射 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<!-- 添加数据源 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
</dependency>
<!-- 配置本地的jar -->
<dependency>
<groupId>ojdbc</groupId>
<artifactId>objdc1</artifactId>
<version>3.2.8</version>
<scope>system</scope>
<systemPath>C:\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar</systemPath>
</dependency>
</dependencies>
<modelVersion>4.0.0</modelVersion>
<groupId>cn.et</groupId>
<artifactId>ssmhboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
mysource.url=jdbc:oracle:thin:localhost:1521:orcl
mysource.username=scott
mysource.password=tiger
mysource.driverClassName=oracle.jdbc.OracleDriver
#spring.jpa.properties.hibernate.dialect =org.hibernate.dialect.Oracle10gDialect
spring.jpa.show-sql=true
logging.level.root=INFO
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=INFO
#这个是传值给前台把他转成 下面那种格式的字符串
spring.jackson.time-zone=GMT+8
spring.jackson.date-format=yyyy-MM-dd
dao
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
/**
* 继承CrudRepository 不要实现
* 查询那些复杂的逻辑要自己写
* @author Administrator
*
*/
public interface EmpJaxaDao extends CrudRepository<EmpEntity, Long>{
/**
* 使用的是hsql
* @param ename
* @return
*/
@Query("select count(*) from EmpEntity where ename like :ename")
public int queryTotalEmp(@Param("ename")String ename);
/*@Query(value="select hibernate_sequence.nextval from dual",nativeQuery=true)
public int selectNext();*/
/**
* 使用sql
* @param ename
* @param startIndex
* @param endIndex
* @return
* @throws Exception
*/
@Query(value="select * from (select t.*,rownum rn from (select * from emp order by empno desc) t " +
"where ename like :ename order by t.empno desc) where rn>=:start and rn<=:end",nativeQuery=true)
public List<EmpEntity> queryPaginEmp(
@Param("ename")String ename,
@Param("start")int start,
@Param("end")int end) throws Exception;
}
service
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class EmpJaxaServiceImpl implements EmpJaxaService {
@Autowired
private EmpJaxaDao empDao;
/**
*调用查询数据库的数据方法
*/
public List<EmpEntity>queryEmp(String ename){
return null;
}
/**
*调用新增数据库的数据方法
*/
public void insertEmp(EmpEntity emp){
empDao.save(emp);
}
/**
*调用更新数据库的数据方法
*/
public void updateEmp(EmpEntity emp){
empDao.save(emp);
}
/**
* 调用删除数据库数据的方法
*/
public void deleteEmp(Long empno){
EmpEntity emp=new EmpEntity();
emp.setEmpno(empno);
empDao.delete(emp);
}
/**
*查询数据库的分页数据方法
*/
public PagingEntity queryPaginEmp(String ename,Integer current) {
if(ename==null){
ename="";
}
int sum=empDao.queryTotalEmp("%"+ename+"%");
PagingEntity p=Calculate.calculate(current, 5, sum);
List<EmpEntity> list=null;
try {
list = empDao.queryPaginEmp("%"+ename+"%",p.getStart(),p.getEnd());
} catch (Exception e) {
e.printStackTrace();
}
p.setList(list);
return p;
}
}
entity
如果要用关系映射就用下面代码
@ManyToOne
/告诉他外键的列名
@JoinColumn(name="deptno")//加入一列作为外键
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.format.annotation.DateTimeFormat;
/**
* 数据库的数据类 (一个对象就是数据库的一行)
* @author Administrator
*AUTO 调用数据库的这个序列HIBERNATE_SEQUENCE自动设置到主键
*/
@Entity
@Table(name="emp")
public class EmpEntity {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long empno;
private String ename;
private String job;
private String mgr;
//日期格式就是用这种格式转换
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date hiredate;
private String sal;
private String comm;
private String deptno;
public Long getEmpno() {
return empno;
}
public void setEmpno(Long empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getMgr() {
return mgr;
}
public void setMgr(String mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public String getSal() {
return sal;
}
public void setSal(String sal) {
this.sal = sal;
}
public String getComm() {
return comm;
}
public void setComm(String comm) {
this.comm = comm;
}
public String getDeptno() {
return deptno;
}
public void setDeptno(String deptno) {
this.deptno = deptno;
}
}
jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript"><!--
var path="${pageContext.request.contextPath}/day0609";
//根据id获取对象
function $(id){
return document.getElementById(id);
}
//和div赋值
function toUpdate(empno,ename,job,mgr,hiredate,sal,comm,deptno){
$('updateDiv').style.display='block';
$('empNoId').value=empno;
$('updateEname').value=(ename=='null'?'':ename);
$('updateJob').value=(job=='null'?'':job);
$('updateMgr').value=(mgr=='null'?'':mgr);
$('updateHiredate').value=(hiredate=='null'?'':hiredate);
$('updateSal').value=(sal=='null'?'':sal);
$('updateComm').value=(comm=='null'?'':comm);
$('updateDeptno').value=(deptno=='null'?'':deptno);
}
/**
封装ajax
**/
function sendAjax(method,url,callback,param){
var xmlHttp=new XMLHttpRequest();
//向服务器发出请求
//规定请求的类型、URL 以及是否异步处理请求。
//method:请求的类型;GET 或 POST
//url:文件在服务器上的位置
//async:true(异步)或 false(同步)
xmlHttp.open(method,url,true);
//设置响应的回调 当后台的json返回后 自动调用xhr.onreadystatechange指定的函数
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
//成功后调用第三个函数
callback(xmlHttp.responseText);
}
}
//post 在send里面带参数 get直接带 post一定要设置setRequestHeader
if(method=="POST"){
xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlHttp.send(param);
}else{
xmlHttp.send();
}
}
//和action交互的方法 用于更新
function toUpdate1(){
document.getElementById('updateDiv').style.display='none'
var empno=$('empNoId').value;
var ename=$('updateEname').value;
var job=$('updateJob').value;
var mgr=$('updateMgr').value;
var hiredate=$('updateHiredate').value;
var sal=$('updateSal').value;
var comm=$('updateComm').value;
var deptno=$('updateDeptno').value;
//参数
var param="ename="+ename+"&job="+job+"&mgr="+mgr+"&hiredate="+hiredate+"&sal="+sal+"&comm="+comm+"&deptno="+deptno+"&empno="+empno
//调用ajax好action交互
sendAjax("POST",path+"/updateEmp",function(message){
if(message==1){
alert("操作成功");
toQuery(current);
}else{
alert("操作失败");
}
},param)
}
//和action交互的方法 用于新增
function toAdd(){
document.getElementById('addDiv').style.display='none'
var xmlHttp=new XMLHttpRequest();
var ename=$('a1').value;
var job=$('a2').value;
var mgr=$('a3').value;
var hiredate=$('a4').value;
var sal=$('a5').value;
var comm=$('a6').value;
var deptno=$('a7').value;
var param="ename="+ename+"&job="+job+"&mgr="+mgr+"&hiredate="+hiredate+"&sal="+sal+"&comm="+comm+"&deptno="+deptno;
sendAjax("POST",path+"/insertEmp",function(message){
if(message==1){
alert("操作成功");
toQuery(current);
}else{
alert("操作失败");
}
},param)
}
//和action交互的方法 用于删除
function toDelete(empno){
var param="empno="+empno;
sendAjax("POST",path+"/deleteEmp",function(message){
if(message==1){
alert("操作成功");
toQuery(current);
}else{
alert("操作失败");
}
},param)
}
//分页的一些参数
var current=1;
var sum=0;
var trail=0;
var last=0;
var next=0;
和action交互的方法 用于查询
function toQuery(pagin){
var xmlHttp=new XMLHttpRequest();
var tb=$("tb");
var td=$("td");
var ename=$("ename").value;
xmlHttp.open("GET",path+"/queryPaginEmp?ename="+ename+"&pagin="+pagin,true);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
//获取action响应的内容
var json=xmlHttp.responseText;
//把它转成json对象
var jsonall=JSON.parse(json);
//给分页的一些参数赋值
current=jsonall.current;
sum=jsonall.sum;
trail=jsonall.trail;
last=jsonall.last;
next=jsonall.next;
$("p1").innerHTML=current;
$("p2").innerHTML=trail;
$("p3").innerHTML=sum;
var jsonObj=jsonall.list;
//无刷新在此查询的时候把整个tbody删除
tb.removeChild(td);
var tdRow=document.createElement("tbody");
tdRow.setAttribute("id","td");
//把tbody加到table里面去
tb.appendChild(tdRow);
for ( var i = 0; i < jsonObj.length; i++) {
var el = jsonObj[i];
//创建节点
var trRow=document.createElement("tr");
var tdRow1=document.createElement("td");
var tdRow2=document.createElement("td");
var tdRow3=document.createElement("td");
var tdRow4=document.createElement("td");
var tdRow5=document.createElement("td");
var tdRow6=document.createElement("td");
var tdRow7=document.createElement("td");
var tdRow8=document.createElement("td");
var tdRow9=document.createElement("td");
tdRow1.innerHTML=el.empno;
tdRow2.innerHTML=el.ename;
tdRow3.innerText=el.job;
tdRow4.innerText=el.mgr;
//这个是装时期对象格式的
//var date=new Date();
//var hiredate;
//if(el.hiredate!=null){
// date.setTime(el.hiredate);
// hiredate=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
//}else{
// hiredate="";
//}
var hiredate=el.hiredate;
tdRow5.innerHTML=hiredate;
tdRow6.innerHTML=el.sal;
tdRow7.innerHTML=el.comm;
tdRow8.innerHTML=el.deptno;
tdRow9.innerHTML="<button οnclick='toDelete("+el.empno+")'>删除</button><button οnclick=toUpdate("+el.empno+",'"+el.ename+"','"+el.job+"','"+
el.mgr+"','"+hiredate+"','"+el.sal+"','"+el.comm+"','"+el.deptno+"')>更新</button>";
//把节点tdRow1加到trRow
trRow.appendChild(tdRow1);
trRow.appendChild(tdRow2);
trRow.appendChild(tdRow3);
trRow.appendChild(tdRow4);
trRow.appendChild(tdRow5);
trRow.appendChild(tdRow6);
trRow.appendChild(tdRow7);
trRow.appendChild(tdRow8);
trRow.appendChild(tdRow9);
tdRow.appendChild(trRow);
}
}
}
xmlHttp.send();
}
--></script>
</head>
<body οnlοad="toQuery(1)">
<!-- http://localhost:8080/day20170609/emp.jsp -->
ename<input type="text" name="ename" id="ename"/>
<input type="button" value="查询" οnclick="toQuery(current)"/>
<input type="button" value="新增" οnclick="document.getElementById('addDiv').style.display='block'"/><br/>
<table id="tb">
<thead>
<tr>
<td>empno</td>
<td>ename</td>
<td>job</td>
<td>mgr</td>
<td>hiredate</td>
<td>sal</td>
<td>comm</td>
<td>deptno</td>
<td>操作</td>
</tr>
</thead >
<tbody id="td">
</tbody>
</table>
<input type="image" src="${pageContext.request.contextPath}/day20170609/image/18.gif" οnclick="toQuery(1)"/>
<input type="image" src="${pageContext.request.contextPath}/day20170609/image/20.gif" οnclick="toQuery(last)"/>
当前<span id="p1"></span>页 总<span id="p2"></span>页 总:<span id="p3"></span>
<input type="image" src="${pageContext.request.contextPath}/day20170609/image/16.gif" οnclick="toQuery(next)"/>
<input type="image" src="${pageContext.request.contextPath}/day20170609/image/14.gif" οnclick="toQuery(trail)"/>
<!-- 新增的div-->
<div id="addDiv" style="display:none;position: absolute;left: 65%;top: 14%;width: 300px;height:200px;background:linear-gradient(to bottom,skyblue,gray);border: 1px solid black;">
ENAME:<input type='text' name='ename' id='a1'>
<br/>
JOB:<input type='text' name='job' id='a2'>
<br/>
MGR:<input type='text' name='mgr' id='a3'>
<br/>
HIREDATE:<input type='text' name='hiredate' id='a4'>
<br/>
SAL:<input type='text' name='sal' id='a5'>
<br/>
COMM:<input type='text' name='comm' id='a6'>
<br/>
DEPTNO:<input type='text' name='deptno' id='a7'>
<br/>
<input type='button' value="新增" οnclick="toAdd()">
<input type='button' value="关闭" οnclick="document.getElementById('addDiv').style.display='none'">
</div>
<!-- 修改的div-->
<div id="updateDiv" style="display:none;position: absolute;left: 65%;top: 14%;width: 300px;height:200px;background:linear-gradient(to bottom,skyblue,gray);border: 1px solid black;">
<input type='hidden' name='empno' id='empNoId'>
ENAME: <input type='text' id="updateEname" name='ename'>
<br/>
JOB: <input type='text' id="updateJob" name='job'>
<br/>
MGR: <input type='text' id="updateMgr" name='mgr'>
<br/>
HIREDATE: <input type='text' id="updateHiredate" name='hiredate'>
<br/>
SAL: <input type='text' id="updateSal" name='sal'>
<br/>
COMM: <input type='text' id="updateComm" name='comm'>
<br/>
DEPTNO: <input type='text' id="updateDeptno" name='deptno'>
<br/>
<input type='button' value='修改' οnclick="toUpdate1()">
<input type='button' value="关闭" οnclick="document.getElementById('updateDiv').style.display='none'">
</div>
</body>
</html>