您的位置:首页 > 其它

7月14日至7月15日心得

2015-07-15 00:00 190 查看
摘要: 主要是学习数据库的增删查改。

两天我们学习了很多内容,先是学习了如何安装数据库,使用数据库的语句。比如:

插入Insert 更新update 和 包括slecte语句和有关搜索的search语句。

大概的代码是:

<select id="selectuser"

parameterClass="cn.training.bean.UserBean"

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

SELECT

user_id as userId,

userName as userName

FROM

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>

然后还有几个数据库关联的地方改了几个数据,以后切记要改。否则无法实现连接。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: