标签:turn service 数据库配置 support ati 使用 ++ col hibernate
在此之前!请在数据库创建一个lib的数据库!
+++++++++++++++++++++++++++++++++++++++++++++++++++++
--------------------------------------------------------------------------------------------
【包名com.nf.action】
(文件名BookAction.java)
package com.nf.action;
import com.nf.entity.Book;
import com.nf.service.BookService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@Scope("prototype")
public class BookAction extends ActionSupport {
//此jsonMap用于存储JSON格式的数据
private Map<String,Object> jsonMap = new HashMap();
public Map<String, Object> getJsonMap() {
return jsonMap;
}
public void setJsonMap(Map<String, Object> jsonMap) {
this.jsonMap = jsonMap;
}
@Autowired
private BookService bookService;
public String getAllBook(){
List<Book> bookList = bookService.getAllBook();
jsonMap.put("bookList", bookList);
return "jsonOK";
}
}
==============================================
----------------------------------------------------------------------------------
【包名com.nf.dao】
{接口名BookDao.java}
package com.nf.dao;
import com.nf.entity.Book;
import java.util.List;
public interface BookDao {
public List<Book> getAllBook();
}
----------------------------------------------------------------------------------
【包名com.nf.dao】
(文件名BookDaoImpl.java)
package com.nf.dao;
import com.nf.entity.Book;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Scope("prototype")
public class BookDaoImpl implements BookDao {
@Autowired
private SessionFactory sessionFactory;
public List<Book> getAllBook() {
Session session = sessionFactory.getCurrentSession();
Query<Book> query = session.createQuery("from Book", Book.class);
List<Book> bookList = query.getResultList();
return bookList;
}
}
==============================================
----------------------------------------------------------------------------------
【com.nf.entity】
(Book.java)
package com.nf.entity;
import javax.persistence.*;
@Entity
@Table(name = "book")
public class Book {
private Integer id;
private String name;
private Integer price;
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name",length = 50,nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "price",nullable = false)
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
}
==============================================
----------------------------------------------------------------------------------
【com.nf.service】
{BookService.java}
package com.nf.service;
import com.nf.entity.Book;
import java.util.List;
public interface BookService {
public List<Book> getAllBook();
}
--------------------------------------------------------------------------------
【com.nf.service】
(BookServiceImpl.java)
package com.nf.service;
import com.nf.dao.BookDao;
import com.nf.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Scope("prototype")
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Transactional
public List<Book> getAllBook() {
List<Book> bookList = bookDao.getAllBook();
//编写游戏规则(业务逻辑代码:没有任何数据库操作)
return bookList;
}
}
////////////////////////////////////////////////////////////////////////
数据库配置(mysql)自己改!
#database information
driverClass=com.mysql.cj.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/lib?serverTimezone=UTC
user=root
password=root
///////////////////////////////////////////////////////////////////////
未完有待编辑!
标签:turn service 数据库配置 support ati 使用 ++ col hibernate
原文地址:http://www.cnblogs.com/Zbaozi/p/7920612.html