struts2+spring+ibatis框架整合实现增删改查

网友投稿 224 2022-12-31


struts2+spring+ibatis框架整合实现增删改查

初步学习SSI框架,做的struts2+spring+ibatis框架整合的小实例,实现增删改查操作。

项目框架如下所示:

准备工作:导入需要的struts2、spring、ibatis的jar包,这里不一一列举了。

下面直接上完整的配置文件内容和代码,不说明先后步骤了

一、配置文件

1、spring配置文件applicationContext.xml(业务逻辑层)

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd">

classpath*:jdbc.properties

destroy-method="close">

classpath*:/sqlMapConfig.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd">

classpath*:jdbc.properties

destroy-method="close">

destroy-method="close">

classpath*:/sqlMapConfig.xml

2、数据库参数配置文件jdbc.properties

jdbc.driverClassName=oracle.jdbc.OracleDriver

jdbc.url=jdbc:oracle:thin:@198.10.3.138:1521:fapdb

jdbc.username=fapdb

jdbc.password=fapdb

3、ibatis配置文件sqlMapConfig.xml(持久层)

"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

enhancementEnabled="true"

lazyLoadingEnabled="false"

maxTransactions="32"

maxRequests="512"

maxSessions="128"

useStatementNamespaces="false"/>

4、实体类sql配置文件User.xml(持久层)

PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"

"http://ibatis.apache.org/dtd/sql-map-2.dtd">

insert into gy_user(USERID,USERNAME,PASSWORD) values(#userid#,#username#,#password#)

select USERID,USERNAME,PASSWORD from gy_user

USERID=#userid#

order by USERID

select USERID,USERNAME,PASSWORD from gy_user where USERID=#id#

delete from gy_user where USERID=#id#

update gy_user set USERID=#userid#,USERNAME=#username#,PASSWORD=#password# where USERID=#userid#

5、struts参数配置文件struts.properties

### 默认编码

struts.i18n.encoding=UTF-8

### 使用spring装配工厂

struts.objectFactory = spring

struts.objectFactory.spring.autoWire = name

struts.objectFactory.spring.useClassCache = true

struts.multipart.parser=jakarta

struts.multipart.saveDir=/tmp

struts.multipart.maxSize=209715200

struts.action.extension=do

struts.serve.static=true

struts.serve.static.browserCache=true

struts.enable.DynamicMethodInvocation = true

struts.enable.SlashesInActionNames = false

struts.tag.altSyntax=true

struts.devMode = false

struts.i18n.reload=true

struts.ui.templateDir=template

struts.ui.templateSuffix=ftl

struts.configuration.xml.reload=ture

struts.ui.theme=simple

struts.velocity.configfile = velocity.properties

struts.url.http.port = 80

struts.url.https.port = 443

struts.url.includeParams = get

struts.dispatcher.parametersWorkaround = false

struts.freemarker.templatesCache=falsemSZEgr

struts.freemarker.beanwrapperCache=false

struts.freemarker.wrapper.altMap=true

struts.xslt.nocache=false

struts.configuration.files=struts-default.xml,struts-plugin.xml,struts.xml

struts.mapper.alwaysSelectFullNamespace=false

6、struts配置文件struts.xml(控制层)

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

/success.jsp

/userResult.jsp

/success.jsp

/userUpdate.jsp

/success.jsp

7、web.xml配置文件

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

index.jsp

contextConfigLocation

classpath*:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

struts

org.apache.struts2.dispatcher.FilterDispatcher

struts

&lhttp://t;url-pattern>*.do

struts

*.action

struts

*.jsp

二、model模型层

实体类User.java

package com.user.model;

public class User {

private int userid;

private String username;

private String password;

public int getUserid() {

return userid;

}

public String getUsername() {

return username;

}

public String getPassword() {

return password;

}

public void setUserid(int userid) {

this.userid = userid;

}

public void setUsername(String username) {

this.username = username;

}

public void setPassword(String password) {

this.password = password;

}

}

三、Action控制层

userAction.java类

package com.user.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

import com.user.model.User;

import com.user.service.UserService;

public class userAction extends ActionSupport{

HttpServletRequest request;

private User user;

private UserService userService;

int id;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public User getUser() {

return user;

}

public void setUser(User user) {

this.user = user;

}

public UserService getUserService() {

return userService;

}

public void setUserService(UserService userService) {

this.userService = userService;

}

public String insertUser() throws Exception{

userService.insertUser(user);

return "success";

}

//查询用户

public String queryUser() throws Exception{

List userlist=userService.queryUser(user);

System.out.println("userAction--size--"+userlist.size());

for(int i=0;i

System.out.println("userAction--"+userlist.get(i).getUserid()+"-"+userlist.get(i).getUsername()+"-"+userlist.get(http://i).getPassword());

}

ActionContext.getContext().getSession().put("userresult", userlist);

System.out.println("*****");

return "userResult";

}

//删除用户

public String deleteUser() throws Exception{

System.out.println("userid:"+id);

userService.deleteUser(id);

return "successdelete";

}

//查询用户by id

public String queryUserById() throws Exception{

System.out.println("userid:"+id);

User user=userService.queryUserById(id);

System.out.println("--userAction--"+user.getUserid()+"--"+user.getUsername()+"--"+user.getPassword());

ActionContext.getContext().getSession().put("result", user);

return "querybyidRes";

}

//修改用户

public String updateUser() throws Exception{

userService.updateUser(user);

return "successupdate";

}

}

四、Service接口+实现(业务逻辑层)

1、Service接口

UserService.java类

package com.user.service;

import java.util.List;

import com.user.model.User;

public interface UserService {

//添加用户

public void insertUser(User user) throws Exception;

//查询用户

public List queryUser(User user) throws Exception;

//查询用户by id

public User queryUserById(int id) throws Exception;

//删除用户

public void deleteUser(int id) throws Exception;

//修改用户

public void updateUser(User user) throws Exception;

}

2、Service接口实现

UserServiceImpl.java类

package com.user.service.impl;

import java.util.List;

import com.user.model.User;

import com.user.service.UserService;

import com.user.dao.UserDao;

public class UserServiceImpl implements UserService{

private UserDao userdao=null;

public UserDao getUserdao() {

return userdao;

}

public void setUserdao(UserDao userdao) {

this.userdao = userdao;

}

//添加用户

public void insertUser(User user) throws Exception {

// TODO Auto-generated method stub

System.out.println("插入数据User开始--->UserService");

userdao.insertUser(user);

System.out.println("插入数据User结束--->UserService");

}

//查询用户

public List queryUser(User user) throws Exception{

System.out.println("查询数据User开始--->UserService");

List userlist=userdao.queryUser(user);

System.out.println("UserService--size--"+userlist.size());

for(int i=0;i

System.out.println("UserService--"+userlist.get(i).getUserid()+"-"+userlist.get(i).getUsername()+"-"+userlist.get(i).getPassword());

}

System.out.println("查询数据User结束--->UserService");

return userlist;

}

//删除用户

public void deleteUser(int id) throws Exception {

// TODO Auto-generated method stub

System.out.println("删除数据User开始--->UserService");

userdao.deleteUser(id);

System.out.println("删除数据User开始--->UserService");

}

//查询用户by id

public User queryUserById(int id) throws Exception {

// TODO Auto-generated method stub

System.out.println("查询数据BY ID开始--->UserService"+"---"+id);

User user=userdao.queryUserById(id);

System.out.println("查询数据BY ID结束--->UserService"+"---"+id);

return user;

}

//修改用户

public void updateUser(User user) throws Exception {

// TODO Auto-generated method stub

System.out.println("更新数据User开始--->UserService");

userdao.updateUser(user);

System.out.println("更新数据User开始--->UserService");

}

}

五、DAO接口+实现(持久层)

1、DAO接口

UserDao.java类

package com.user.dao;

import java.util.List;

import com.user.model.User;;

public interface UserDao {

//添加用户

public void insertUser(User user);

//查询用户

public List queryUser(User user);

//查询用户by id

public User queryUserById(int id);

//删除用户

public void deleteUser(int id);

//修改用户

public void updateUser(User user);

}

2、DAO接口实现

UserServiceImpl.java类

package com.user.dao.impl;

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.user.dao.UserDao;

import com.user.model.User;

public class UserDaoImpl extends SqlMapClientDaoSupport implements UserDao{

//添加用户

public void insertUser(User user ){

System.out.println("插入数据User开始--->UserDao"+"---"+user.getUserid()+"-"+user.getUsername()+"-"+user.getPassword());

this.getSqlMapClientTemplate().insert("insertUser",user);

System.out.println("插入数据User结束--->UserDao");

}

//查询用户

public List queryUser(User user){

System.out.println("查询数据User开始--->UserDao"+"---"+user.getUserid()+"-"+user.getUsername()+"-"+user.getPassword());

List userlist=(List)this.getSqlMapClientTemplate().queryForList("queryUser", user);

System.out.println("查询数据User结束--->UserDao");

return userlist;

}

//删除用户

public void deleteUser(int id) {

System.out.println("删除数据User开始--->UserDao"+"---"+id);

this.getSqlMapClientTemplate().delete("deleteUser", id);

System.out.println("删除数据User结束--->UserDao"+"---"+id);

}

//查询用户by id

public User queryUserById(int id) {

System.out.println("查询数据BY ID开始--->UserDao"+"---"+id);

User user=(User)this.getSqlMapClientTemplate().queryForObject("queryUserById", id);

System.out.println("查询数据BY ID结束--->UserDao"+"---"+id);

return user;

}

//修改用户

public void updateUser(User user) {

System.out.println("更新数据User开始--->UserDao"+"---"+user.getUserid());

this.getSqlMapClientTemplate().update("updateUser", user);

System.out.println("更新数据User结束--->UserDao"+"---"+user.getUserid());

}

}

六、JSP文件(表示层)

1、index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

添加用户

查询/管理用户

2、query.jsp查询页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

enhancementEnabled="true"

lazyLoadingEnabled="false"

maxTransactions="32"

maxRequests="512"

maxSessions="128"

useStatementNamespaces="false"/>

4、实体类sql配置文件User.xml(持久层)

PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"

"http://ibatis.apache.org/dtd/sql-map-2.dtd">

insert into gy_user(USERID,USERNAME,PASSWORD) values(#userid#,#username#,#password#)

select USERID,USERNAME,PASSWORD from gy_user

USERID=#userid#

order by USERID

select USERID,USERNAME,PASSWORD from gy_user where USERID=#id#

delete from gy_user where USERID=#id#

update gy_user set USERID=#userid#,USERNAME=#username#,PASSWORD=#password# where USERID=#userid#

5、struts参数配置文件struts.properties

### 默认编码

struts.i18n.encoding=UTF-8

### 使用spring装配工厂

struts.objectFactory = spring

struts.objectFactory.spring.autoWire = name

struts.objectFactory.spring.useClassCache = true

struts.multipart.parser=jakarta

struts.multipart.saveDir=/tmp

struts.multipart.maxSize=209715200

struts.action.extension=do

struts.serve.static=true

struts.serve.static.browserCache=true

struts.enable.DynamicMethodInvocation = true

struts.enable.SlashesInActionNames = false

struts.tag.altSyntax=true

struts.devMode = false

struts.i18n.reload=true

struts.ui.templateDir=template

struts.ui.templateSuffix=ftl

struts.configuration.xml.reload=ture

struts.ui.theme=simple

struts.velocity.configfile = velocity.properties

struts.url.http.port = 80

struts.url.https.port = 443

struts.url.includeParams = get

struts.dispatcher.parametersWorkaround = false

struts.freemarker.templatesCache=falsemSZEgr

struts.freemarker.beanwrapperCache=false

struts.freemarker.wrapper.altMap=true

struts.xslt.nocache=false

struts.configuration.files=struts-default.xml,struts-plugin.xml,struts.xml

struts.mapper.alwaysSelectFullNamespace=false

6、struts配置文件struts.xml(控制层)

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

/success.jsp

/userResult.jsp

/success.jsp

/userUpdate.jsp

/success.jsp

7、web.xml配置文件

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

index.jsp

contextConfigLocation

classpath*:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

struts

org.apache.struts2.dispatcher.FilterDispatcher

struts

&lhttp://t;url-pattern>*.do

struts

*.action

struts

*.jsp

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

index.jsp

contextConfigLocation

classpath*:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

struts

org.apache.struts2.dispatcher.FilterDispatcher

struts

&lhttp://t;url-pattern>*.do

struts

*.action

struts

*.jsp

二、model模型层

实体类User.java

package com.user.model;

public class User {

private int userid;

private String username;

private String password;

public int getUserid() {

return userid;

}

public String getUsername() {

return username;

}

public String getPassword() {

return password;

}

public void setUserid(int userid) {

this.userid = userid;

}

public void setUsername(String username) {

this.username = username;

}

public void setPassword(String password) {

this.password = password;

}

}

三、Action控制层

userAction.java类

package com.user.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

import com.user.model.User;

import com.user.service.UserService;

public class userAction extends ActionSupport{

HttpServletRequest request;

private User user;

private UserService userService;

int id;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public User getUser() {

return user;

}

public void setUser(User user) {

this.user = user;

}

public UserService getUserService() {

return userService;

}

public void setUserService(UserService userService) {

this.userService = userService;

}

public String insertUser() throws Exception{

userService.insertUser(user);

return "success";

}

//查询用户

public String queryUser() throws Exception{

List userlist=userService.queryUser(user);

System.out.println("userAction--size--"+userlist.size());

for(int i=0;i

System.out.println("userAction--"+userlist.get(i).getUserid()+"-"+userlist.get(i).getUsername()+"-"+userlist.get(http://i).getPassword());

}

ActionContext.getContext().getSession().put("userresult", userlist);

System.out.println("*****");

return "userResult";

}

//删除用户

public String deleteUser() throws Exception{

System.out.println("userid:"+id);

userService.deleteUser(id);

return "successdelete";

}

//查询用户by id

public String queryUserById() throws Exception{

System.out.println("userid:"+id);

User user=userService.queryUserById(id);

System.out.println("--userAction--"+user.getUserid()+"--"+user.getUsername()+"--"+user.getPassword());

ActionContext.getContext().getSession().put("result", user);

return "querybyidRes";

}

//修改用户

public String updateUser() throws Exception{

userService.updateUser(user);

return "successupdate";

}

}

四、Service接口+实现(业务逻辑层)

1、Service接口

UserService.java类

package com.user.service;

import java.util.List;

import com.user.model.User;

public interface UserService {

//添加用户

public void insertUser(User user) throws Exception;

//查询用户

public List queryUser(User user) throws Exception;

//查询用户by id

public User queryUserById(int id) throws Exception;

//删除用户

public void deleteUser(int id) throws Exception;

//修改用户

public void updateUser(User user) throws Exception;

}

2、Service接口实现

UserServiceImpl.java类

package com.user.service.impl;

import java.util.List;

import com.user.model.User;

import com.user.service.UserService;

import com.user.dao.UserDao;

public class UserServiceImpl implements UserService{

private UserDao userdao=null;

public UserDao getUserdao() {

return userdao;

}

public void setUserdao(UserDao userdao) {

this.userdao = userdao;

}

//添加用户

public void insertUser(User user) throws Exception {

// TODO Auto-generated method stub

System.out.println("插入数据User开始--->UserService");

userdao.insertUser(user);

System.out.println("插入数据User结束--->UserService");

}

//查询用户

public List queryUser(User user) throws Exception{

System.out.println("查询数据User开始--->UserService");

List userlist=userdao.queryUser(user);

System.out.println("UserService--size--"+userlist.size());

for(int i=0;i

System.out.println("UserService--"+userlist.get(i).getUserid()+"-"+userlist.get(i).getUsername()+"-"+userlist.get(i).getPassword());

}

System.out.println("查询数据User结束--->UserService");

return userlist;

}

//删除用户

public void deleteUser(int id) throws Exception {

// TODO Auto-generated method stub

System.out.println("删除数据User开始--->UserService");

userdao.deleteUser(id);

System.out.println("删除数据User开始--->UserService");

}

//查询用户by id

public User queryUserById(int id) throws Exception {

// TODO Auto-generated method stub

System.out.println("查询数据BY ID开始--->UserService"+"---"+id);

User user=userdao.queryUserById(id);

System.out.println("查询数据BY ID结束--->UserService"+"---"+id);

return user;

}

//修改用户

public void updateUser(User user) throws Exception {

// TODO Auto-generated method stub

System.out.println("更新数据User开始--->UserService");

userdao.updateUser(user);

System.out.println("更新数据User开始--->UserService");

}

}

五、DAO接口+实现(持久层)

1、DAO接口

UserDao.java类

package com.user.dao;

import java.util.List;

import com.user.model.User;;

public interface UserDao {

//添加用户

public void insertUser(User user);

//查询用户

public List queryUser(User user);

//查询用户by id

public User queryUserById(int id);

//删除用户

public void deleteUser(int id);

//修改用户

public void updateUser(User user);

}

2、DAO接口实现

UserServiceImpl.java类

package com.user.dao.impl;

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.user.dao.UserDao;

import com.user.model.User;

public class UserDaoImpl extends SqlMapClientDaoSupport implements UserDao{

//添加用户

public void insertUser(User user ){

System.out.println("插入数据User开始--->UserDao"+"---"+user.getUserid()+"-"+user.getUsername()+"-"+user.getPassword());

this.getSqlMapClientTemplate().insert("insertUser",user);

System.out.println("插入数据User结束--->UserDao");

}

//查询用户

public List queryUser(User user){

System.out.println("查询数据User开始--->UserDao"+"---"+user.getUserid()+"-"+user.getUsername()+"-"+user.getPassword());

List userlist=(List)this.getSqlMapClientTemplate().queryForList("queryUser", user);

System.out.println("查询数据User结束--->UserDao");

return userlist;

}

//删除用户

public void deleteUser(int id) {

System.out.println("删除数据User开始--->UserDao"+"---"+id);

this.getSqlMapClientTemplate().delete("deleteUser", id);

System.out.println("删除数据User结束--->UserDao"+"---"+id);

}

//查询用户by id

public User queryUserById(int id) {

System.out.println("查询数据BY ID开始--->UserDao"+"---"+id);

User user=(User)this.getSqlMapClientTemplate().queryForObject("queryUserById", id);

System.out.println("查询数据BY ID结束--->UserDao"+"---"+id);

return user;

}

//修改用户

public void updateUser(User user) {

System.out.println("更新数据User开始--->UserDao"+"---"+user.getUserid());

this.getSqlMapClientTemplate().update("updateUser", user);

System.out.println("更新数据User结束--->UserDao"+"---"+user.getUserid());

}

}

六、JSP文件(表示层)

1、index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

System.out.println("userAction--"+userlist.get(i).getUserid()+"-"+userlist.get(i).getUsername()+"-"+userlist.get(http://i).getPassword());

}

ActionContext.getContext().getSession().put("userresult", userlist);

System.out.println("*****");

return "userResult";

}

//删除用户

public String deleteUser() throws Exception{

System.out.println("userid:"+id);

userService.deleteUser(id);

return "successdelete";

}

//查询用户by id

public String queryUserById() throws Exception{

System.out.println("userid:"+id);

User user=userService.queryUserById(id);

System.out.println("--userAction--"+user.getUserid()+"--"+user.getUsername()+"--"+user.getPassword());

ActionContext.getContext().getSession().put("result", user);

return "querybyidRes";

}

//修改用户

public String updateUser() throws Exception{

userService.updateUser(user);

return "successupdate";

}

}

四、Service接口+实现(业务逻辑层)

1、Service接口

UserService.java类

package com.user.service;

import java.util.List;

import com.user.model.User;

public interface UserService {

//添加用户

public void insertUser(User user) throws Exception;

//查询用户

public List queryUser(User user) throws Exception;

//查询用户by id

public User queryUserById(int id) throws Exception;

//删除用户

public void deleteUser(int id) throws Exception;

//修改用户

public void updateUser(User user) throws Exception;

}

2、Service接口实现

UserServiceImpl.java类

package com.user.service.impl;

import java.util.List;

import com.user.model.User;

import com.user.service.UserService;

import com.user.dao.UserDao;

public class UserServiceImpl implements UserService{

private UserDao userdao=null;

public UserDao getUserdao() {

return userdao;

}

public void setUserdao(UserDao userdao) {

this.userdao = userdao;

}

//添加用户

public void insertUser(User user) throws Exception {

// TODO Auto-generated method stub

System.out.println("插入数据User开始--->UserService");

userdao.insertUser(user);

System.out.println("插入数据User结束--->UserService");

}

//查询用户

public List queryUser(User user) throws Exception{

System.out.println("查询数据User开始--->UserService");

List userlist=userdao.queryUser(user);

System.out.println("UserService--size--"+userlist.size());

for(int i=0;i

System.out.println("UserService--"+userlist.get(i).getUserid()+"-"+userlist.get(i).getUsername()+"-"+userlist.get(i).getPassword());

}

System.out.println("查询数据User结束--->UserService");

return userlist;

}

//删除用户

public void deleteUser(int id) throws Exception {

// TODO Auto-generated method stub

System.out.println("删除数据User开始--->UserService");

userdao.deleteUser(id);

System.out.println("删除数据User开始--->UserService");

}

//查询用户by id

public User queryUserById(int id) throws Exception {

// TODO Auto-generated method stub

System.out.println("查询数据BY ID开始--->UserService"+"---"+id);

User user=userdao.queryUserById(id);

System.out.println("查询数据BY ID结束--->UserService"+"---"+id);

return user;

}

//修改用户

public void updateUser(User user) throws Exception {

// TODO Auto-generated method stub

System.out.println("更新数据User开始--->UserService");

userdao.updateUser(user);

System.out.println("更新数据User开始--->UserService");

}

}

五、DAO接口+实现(持久层)

1、DAO接口

UserDao.java类

package com.user.dao;

import java.util.List;

import com.user.model.User;;

public interface UserDao {

//添加用户

public void insertUser(User user);

//查询用户

public List queryUser(User user);

//查询用户by id

public User queryUserById(int id);

//删除用户

public void deleteUser(int id);

//修改用户

public void updateUser(User user);

}

2、DAO接口实现

UserServiceImpl.java类

package com.user.dao.impl;

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.user.dao.UserDao;

import com.user.model.User;

public class UserDaoImpl extends SqlMapClientDaoSupport implements UserDao{

//添加用户

public void insertUser(User user ){

System.out.println("插入数据User开始--->UserDao"+"---"+user.getUserid()+"-"+user.getUsername()+"-"+user.getPassword());

this.getSqlMapClientTemplate().insert("insertUser",user);

System.out.println("插入数据User结束--->UserDao");

}

//查询用户

public List queryUser(User user){

System.out.println("查询数据User开始--->UserDao"+"---"+user.getUserid()+"-"+user.getUsername()+"-"+user.getPassword());

List userlist=(List)this.getSqlMapClientTemplate().queryForList("queryUser", user);

System.out.println("查询数据User结束--->UserDao");

return userlist;

}

//删除用户

public void deleteUser(int id) {

System.out.println("删除数据User开始--->UserDao"+"---"+id);

this.getSqlMapClientTemplate().delete("deleteUser", id);

System.out.println("删除数据User结束--->UserDao"+"---"+id);

}

//查询用户by id

public User queryUserById(int id) {

System.out.println("查询数据BY ID开始--->UserDao"+"---"+id);

User user=(User)this.getSqlMapClientTemplate().queryForObject("queryUserById", id);

System.out.println("查询数据BY ID结束--->UserDao"+"---"+id);

return user;

}

//修改用户

public void updateUser(User user) {

System.out.println("更新数据User开始--->UserDao"+"---"+user.getUserid());

this.getSqlMapClientTemplate().update("updateUser", user);

System.out.println("更新数据User结束--->UserDao"+"---"+user.getUserid());

}

}

六、JSP文件(表示层)

1、index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

System.out.println("UserService--"+userlist.get(i).getUserid()+"-"+userlist.get(i).getUsername()+"-"+userlist.get(i).getPassword());

}

System.out.println("查询数据User结束--->UserService");

return userlist;

}

//删除用户

public void deleteUser(int id) throws Exception {

// TODO Auto-generated method stub

System.out.println("删除数据User开始--->UserService");

userdao.deleteUser(id);

System.out.println("删除数据User开始--->UserService");

}

//查询用户by id

public User queryUserById(int id) throws Exception {

// TODO Auto-generated method stub

System.out.println("查询数据BY ID开始--->UserService"+"---"+id);

User user=userdao.queryUserById(id);

System.out.println("查询数据BY ID结束--->UserService"+"---"+id);

return user;

}

//修改用户

public void updateUser(User user) throws Exception {

// TODO Auto-generated method stub

System.out.println("更新数据User开始--->UserService");

userdao.updateUser(user);

System.out.println("更新数据User开始--->UserService");

}

}

五、DAO接口+实现(持久层)

1、DAO接口

UserDao.java类

package com.user.dao;

import java.util.List;

import com.user.model.User;;

public interface UserDao {

//添加用户

public void insertUser(User user);

//查询用户

public List queryUser(User user);

//查询用户by id

public User queryUserById(int id);

//删除用户

public void deleteUser(int id);

//修改用户

public void updateUser(User user);

}

2、DAO接口实现

UserServiceImpl.java类

package com.user.dao.impl;

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.user.dao.UserDao;

import com.user.model.User;

public class UserDaoImpl extends SqlMapClientDaoSupport implements UserDao{

//添加用户

public void insertUser(User user ){

System.out.println("插入数据User开始--->UserDao"+"---"+user.getUserid()+"-"+user.getUsername()+"-"+user.getPassword());

this.getSqlMapClientTemplate().insert("insertUser",user);

System.out.println("插入数据User结束--->UserDao");

}

//查询用户

public List queryUser(User user){

System.out.println("查询数据User开始--->UserDao"+"---"+user.getUserid()+"-"+user.getUsername()+"-"+user.getPassword());

List userlist=(List)this.getSqlMapClientTemplate().queryForList("queryUser", user);

System.out.println("查询数据User结束--->UserDao");

return userlist;

}

//删除用户

public void deleteUser(int id) {

System.out.println("删除数据User开始--->UserDao"+"---"+id);

this.getSqlMapClientTemplate().delete("deleteUser", id);

System.out.println("删除数据User结束--->UserDao"+"---"+id);

}

//查询用户by id

public User queryUserById(int id) {

System.out.println("查询数据BY ID开始--->UserDao"+"---"+id);

User user=(User)this.getSqlMapClientTemplate().queryForObject("queryUserById", id);

System.out.println("查询数据BY ID结束--->UserDao"+"---"+id);

return user;

}

//修改用户

public void updateUser(User user) {

System.out.println("更新数据User开始--->UserDao"+"---"+user.getUserid());

this.getSqlMapClientTemplate().update("updateUser", user);

System.out.println("更新数据User结束--->UserDao"+"---"+user.getUserid());

}

}

六、JSP文件(表示层)

1、index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

添加用户

查询/管理用户

2、query.jsp查询页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

用户ID:

3、userAdd.jsp添加用户页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

4、userUpdate.jsp修改用户页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ page import="com.user.model.User" ;%>

<%

User user=(User)session.getAttribute("result");

%>

5、userResult.jsp用户列表

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ page import="com.user.model.User" ;%>

<%

List userlist=(List)session.getAttribute("userresult");

System.out.println("userResult.jsp--size--"+userlist.size());

for(int i=0;i

System.out.println("userResult.jsp--"+userlist.get(i).getUserid()+"-"+userlist.get(i).getUsername()+"-"+userlist.get(i).getPassword());

}

%>

<%for(int i=0;i

<%} %>

用户ID用户名密码修改删除
<%=userlist.get(i).getUserid() %><%=userlist.get(i).getUsername() %><%=userlist.get(i).getPassword() %>修改删除

System.out.println("userResult.jsp--"+userlist.get(i).getUserid()+"-"+userlist.get(i).getUsername()+"-"+userlist.get(i).getPassword());

}

%>

<%for(int i=0;i

<%} %>

6、success.jsp操作成功页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

操作成功!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微服务网关选择(微服务架构 网关)
下一篇:java实现点击按钮事件弹出子窗口
相关文章

 发表评论

暂时没有评论,来抢沙发吧~