我的开发用到了struts和hibernate 数据库是用MYSQL,里面的一个数据表创建如下
在MYSQL的数据库中没有INT类型,所以只能用Integer类型,但在java中Integer中不是基本数据类型,
所以要对Integer的数据进行操作就有点小麻烦,我开始没注意到这个问题,后来找到Integer的构造方法
把要操作的int数据传递给这个构造方法获取对应的Integer对象,在对数据库进行操作。
在MYSQL的数据库中没有INT类型,所以只能用Integer类型,但在java中Integer中不是基本数据类型,
所以要对Integer的数据进行操作就有点小麻烦,我开始没注意到这个问题,后来找到Integer的构造方法
把要操作的int数据传递给这个构造方法获取对应的Integer对象,在对数据库进行操作。
-------------------------
问题总结:出现这个问题还是自己的对java数据类型操作的不熟练,还需要继续努力。
-------------------------
数据库创建代码
- CREATE TABLE `users` (
- `id` int(10) unsigned NOT NULL auto_increment,
- `username` varchar(25) default NULL,
- `password` varchar(20) default NULL,
- `first_name` varchar(15) default NULL,
- `last_name` varchar(20) default NULL,
- `date_create` time default NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=gb2312;
下面是一个UsersDAO.JAVA 在这个里面有 public Users findById(java.lang.Integer id) 方法,
我的另外一个程序要调用这个类的fingById方法,
要传递一个Integer对象,怎么传递这个对象进来呢
用如下代码这里我传递i的值是2,用到了
Integer的一个构造方法
---------------------------------
public Integer(int value)
- 构造一个新分配的
Integer
对象,它表示指定的int
值。- 参数:
value
-Integer
对象表示的值。
---------------------------------
UsersDAO.JAVA 文件代码如下:
- package com.yourcompany.hibernate;
- import java.util.Date;
- import java.util.List;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.hibernate.LockMode;
- import org.hibernate.Query;
- import org.hibernate.criterion.Example;
- /**
- * A data access object (DAO) providing persistence and search support for Users
- * entities. Transaction control of the save(), update() and delete() operations
- * can directly support Spring container-managed transactions or they can be
- * augmented to handle user-managed Spring transactions. Each of these methods
- * provides additional information for how to configure it for the desired type
- * of transaction control.
- *
- * @see com.yourcompany.hibernate.Users
- * @author MyEclipse Persistence Tools
- */
- public class UsersDAO extends BaseHibernateDAO {
- private static final Log log = LogFactory.getLog(UsersDAO.class);
- // property constants
- public static final String USERNAME = "username";
- public static final String PASSWORD = "password";
- public static final String FIRST_NAME = "firstName";
- public static final String LAST_NAME = "lastName";
- public void save(Users transientInstance) {
- log.debug("saving Users instance");
- try {
- getSession().save(transientInstance);
- log.debug("save successful");
- } catch (RuntimeException re) {
- log.error("save failed", re);
- throw re;
- }
- }
- public void delete(Users persistentInstance) {
- log.debug("deleting Users instance");
- try {
- getSession().delete(persistentInstance);
- log.debug("delete successful");
- } catch (RuntimeException re) {
- log.error("delete failed", re);
- throw re;
- }
- }
- public Users findById(java.lang.Integer id) {
- log.debug("getting Users instance with id: " + id);
- try {
- Users instance = (Users) getSession().get(
- "com.yourcompany.hibernate.Users", id);
- return instance;
- } catch (RuntimeException re) {
- log.error("get failed", re);
- throw re;
- }
- }
- public List findByExample(Users instance) {
- log.debug("finding Users instance by example");
- try {
- List results = getSession().createCriteria(
- "com.yourcompany.hibernate.Users").add(
- Example.create(instance)).list();
- log.debug("find by example successful, result size: "
- + results.size());
- return results;
- } catch (RuntimeException re) {
- log.error("find by example failed", re);
- throw re;
- }
- }
- public List findByProperty(String propertyName, Object value) {
- log.debug("finding Users instance with property: " + propertyName
- + ", value: " + value);
- try {
- String queryString = "from Users as model where model."
- + propertyName + "= ?";
- Query queryObject = getSession().createQuery(queryString);
- queryObject.setParameter(0, value);
- return queryObject.list();
- } catch (RuntimeException re) {
- log.error("find by property name failed", re);
- throw re;
- }
- }
- public List findByUsername(Object username) {
- return findByProperty(USERNAME, username);
- }
- public List findByPassword(Object password) {
- return findByProperty(PASSWORD, password);
- }
- public List findByFirstName(Object firstName) {
- return findByProperty(FIRST_NAME, firstName);
- }
- public List findByLastName(Object lastName) {
- return findByProperty(LAST_NAME, lastName);
- }
- public List findAll() {
- log.debug("finding all Users instances");
- try {
- String queryString = "from Users";
- Query queryObject = getSession().createQuery(queryString);
- return queryObject.list();
- } catch (RuntimeException re) {
- log.error("find all failed", re);
- throw re;
- }
- }
- public Users merge(Users detachedInstance) {
- log.debug("merging Users instance");
- try {
- Users result = (Users) getSession().merge(detachedInstance);
- log.debug("merge successful");
- return result;
- } catch (RuntimeException re) {
- log.error("merge failed", re);
- throw re;
- }
- }
- public void attachDirty(Users instance) {
- log.debug("attaching dirty Users instance");
- try {
- getSession().saveOrUpdate(instance);
- log.debug("attach successful");
- } catch (RuntimeException re) {
- log.error("attach failed", re);
- throw re;
- }
- }
- public void attachClean(Users instance) {
- log.debug("attaching clean Users instance");
- try {
- getSession().lock(instance, LockMode.NONE);
- log.debug("attach successful");
- } catch (RuntimeException re) {
- log.error("attach failed", re);
- throw re;
- }
- }
- }