下面进行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 = sess