7月14日至7月15日心得

兩天咱們學習了不少內容,先是學習瞭如何安裝數據庫,使用數據庫的語句。好比:html

插入Insert 更新update 和 包括slecte語句和有關搜索的search語句。java

大概的代碼是:web

<select id="selectuser"spring

parameterClass="cn.training.bean.UserBean"數據庫

resultClass="cn.training.bean.UserBean">app

SELECTjsp

user_id as userId,post

userName as userName學習

FROMui

user

WHERE

user_id = #userId#

</select>

<insert id="insertuser"

parameterClass="cn.training.bean.UserBean">

insert into user(user_id) values(#userId#)

</insert>

<update id="updateuser"

parameterClass="cn.training.bean.UserBean">

update user set userName=#userName#

WHERE

user_id = #userId#

</update>

爲了要實現增刪查改,咱們創建了三個包 分別是userBean service和controller

在userBean包裏的類中咱們寫了以下代碼:

package cn.training.bean;


public class UserBean {

private String userId;


public String getUserId() {

return userId;

}


public void setUserId(String userId) {

this.userId = userId;

}

private String userName;


public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;


}

}

在service包裏的類中咱們寫了以下代碼:

package cn.training.service;


import jp.terasoluna.fw.dao.QueryDAO;

import jp.terasoluna.fw.dao.UpdateDAO;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.web.bind.annotation.RequestMapping;


import cn.training.bean.UserBean;


@Service

public class HelloWorldService {


@Autowired

QueryDAO queryDao;


@Autowired

UpdateDAO updateDao;


public UserBean searchUser(UserBean frm) {

frm = queryDao.executeForObject("user.selectuser", frm, UserBean.class);

//frm.setuserId("test");

return frm;

}

public int insert(UserBean frm){

int result=0;

try {

result=updateDao.execute("user.insertuser", frm);

}catch(Exception e){

return result;

}

return result;

}

public int updateUser(UserBean frm) {

return updateDao.execute("user.updateuser", frm);

}

}


在controller包裏的類中咱們寫了以下代碼:

package cn.training.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 org.springframework.web.bind.annotation.RequestMethod;


import cn.training.bean.UserBean;

import cn.training.service.HelloWorldService;



@Controller("HelloWorldController")

@RequestMapping("/")

public class HelloWorldController {


@Autowired

HelloWorldService helloWorldService;

    @RequestMapping(value = "/", method = RequestMethod.GET)

    public String index(Model model) {

        return "helloWorld";

    }

    

    @RequestMapping(value = "/init", method = RequestMethod.POST,params="search")

    public String initLogin(UserBean userBean, Model model) {

    UserBean result = helloWorldService.searchUser(userBean);

    model.addAttribute("userBean", result);

        return "login";

    }

    @RequestMapping(value = "/init", method = RequestMethod.POST,params="insert")

    public String inithand(UserBean userinsert, Model model){

    int insert = helloWorldService.insert(userinsert);

    UserBean result=new UserBean();

model.addAttribute("userBean",result);

    if(insert==1){

        model.addAttribute("message","HaHa!");

    return "login";

    }

    else{

    model.addAttribute("message","Defult!");

    return "login";

   

    }

      

    }

    @RequestMapping(value = "/initupdate", method = RequestMethod.POST,params="update")

    public String initsome(UserBean userupdate, Model model){

int update = helloWorldService.updateUser(userupdate);

model.addAttribute("userBean",userupdate);

    return "login";

}

    }

具體的傳值方式看小帥博客。


而爲了實如今前臺頁面的值的輸入和獲取咱們創建了兩個jsp頁面:

首先是login頁面:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<form action="initupdate" method="POST">

 <input name="userId" type="text" value="${userBean.userId}">

 <input name="userName" type="text" value="${userBean.userName}">

${message}

<button type="submit" name="update">更改</button>

</form>

</body>

</html>

其次和HelloWorld頁面: 

固然名字無所謂,代碼以下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<form action="init" method="post">

<input name="userId" type="text">

<button type="submit" name="insert">插入</button>

<button type="submit" name="search">搜索</button>

</form>

</body>

</html>

而後還有幾個數據庫關聯的地方改了幾個數據,之後切記要改。不然沒法實現鏈接。

相關文章
相關標籤/搜索