概述
下面进行username和password向数据库中插入的操作,与前面的操作基本相同。
1.在Address中添加addUsers方法:
package Service;
import com.dojo.UserEntity;
import com.dojo.UsersEntity;
import java.util.List;
public interface Address {
void add(UserEntity ue);
List<UserEntity> selectAll();
boolean searchUsername(String username);
boolean addUsers(UsersEntity usersEntity);
}
2.在AddressImpl中写实现方法:
package Service;
import com.dojo.UserEntity;
import com.dojo.UsersEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.List;
public class AddressImpl implements Address{
@Qualifier("sessionFactory")
@Autowired
private SessionFactory sessionFactory;
@Override
public void add(UserEntity ue){
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(ue);
tx.commit();
}
@Override
public List<UserEntity> selectAll(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Query query = session.createQuery("from UserEntity ");
List<UserEntity> list = query.list();
transaction.commit();
return list;
}
@Override
public boolean searchUsername(String username){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Query query = session.createQuery("select count(*) from UsersEntity as u where u.username=?0").setParameter(0,username);
transaction.commit();
Number number = (Number)query.uniqueResult();
if(number.intValue()>0){
return true;
}else{
return false;
}
}
@Override
public boolean addUsers(UsersEntity usersEntity){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.save(usersEntity);
transaction.commit();
return true;
}
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
}
3.在IOutputGenerator中添加addUsers方法,用来将用户名和密码转换成UsersEntity的格式:
package com.yiibai.output;
public interface IOutputGenerator {
String generateOutput(String uname);
boolean generateSearchUsername(String username);
boolean addUsers(String username,String password);
}
4.在CsvOutputGenerator中添加该方法的实现:
package com.yiibai.output.impl;
import Service.Address;
import Service.AddressImpl;
import com.dojo.UserEntity;
import com.dojo.UsersEntity;
import com.yiibai.output.IOutputGenerator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CsvOutputGenerator implements IOutputGenerator {
@Override
public String generateOutput(String uname){
ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"spring-config.xml"});
Address address = (AddressImpl)ac.getBean("address");
UserEntity userEntity = new UserEntity();
userEntity.setUname(uname);
address.add(userEntity);
return "SUCCESS";
}
@Override
public boolean generateSearchUsername(String username){
ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"spring-config.xml"});
Address address = (AddressImpl)ac.getBean("address");
return address.searchUsername(username);
}
@Override
public boolean addUsers(String username,String password){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"spring-config.xml"});
Address address = (AddressImpl)applicationContext.getBean("address");
UsersEntity usersEntity = new UsersEntity();
usersEntity.setUsername(username);
usersEntity.setPassword(password);
return address.addUsers(usersEntity);
}
}
5.在OutputHelper中添加与action交互的类
package com.yiibai.output;
public class OutputHelper {
IOutputGenerator outputGenerator;
public String generateOutput(String uname){
return outputGenerator.generateOutput(uname);
}
public boolean generateSearchUsername(String username){ return outputGenerator.generateSearchUsername(username);}
public boolean addUsers(String username,String password){ return outputGenerator.addUsers(username,password);}
public void setOutputGenerator(IOutputGenerator outputGenerator){
this.outputGenerator = outputGenerator;
}
}
6.最后在RegisterAction中调用,并将结果传递给页面:
package com.kay.struts2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.yiibai.output.OutputHelper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RegisterAction extends ActionSupport {
private String username;
private String password;
private String result;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute(){
String username = this.getUsername();
String password = this.getPassword();
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring-config.xml"});
OutputHelper output = (OutputHelper)context.getBean("OutputHelper");
if(output.generateSearchUsername(username)){
this.setResult("already");
}else{
boolean b = output.addUsers(username,password);
if(b){
this.setResult("success");
}else{
this.setResult("error");
}
}
return SUCCESS;
}
public String registerPage(){
return SUCCESS;
}
}
运行tomcat,添加数据:
成功。
最后
以上就是还单身老师为你收集整理的IntelliJ IDEA 搭建一个比较完整的网站实例 10的全部内容,希望文章能够帮你解决IntelliJ IDEA 搭建一个比较完整的网站实例 10所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复