Created
April 21, 2020 06:23
-
-
Save Pudding124/bf9444322719d588a108f2532a5f2e09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dao; | |
import model.Car; | |
import org.hibernate.Query; | |
import org.hibernate.Session; | |
import org.hibernate.Transaction; | |
import util.HibernateUtil; | |
public class CarDaoImpl implements CarDao { | |
public void save(Car car) { | |
Session session = HibernateUtil.getSessionFactory().openSession(); | |
Transaction tx = session.beginTransaction(); | |
car.setName(car.getName()); | |
car.setNumber(car.getNumber()); | |
session.save(car); | |
tx.commit(); | |
} | |
public Car carQuery(String number) { | |
Session session = HibernateUtil.getSessionFactory().openSession(); | |
Query query = session.createQuery("from Car c where c.number=?"); | |
// 設定查詢參數 | |
query.setString(0, number); | |
Car car = (Car) query.uniqueResult(); | |
return car; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment