SpringMVC+Spring+Hibernate+Oracle 实现图书管理(CRUD)

来源:互联网 发布:淘宝达人个人中心 编辑:程序博客网 时间:2024/05/29 06:57

1.开发工具:Tomcat8+Oralce11+Eclipse-EE


2.项目工程图:



3.图书表:

图书属性:id 书名 作者 出版社 出版年份 简介 类别

建表语句:

create table book(
id number(10) primary key not null,
title varchar2(50) not null,
author varchar2(20) not null,
press varchar2(50) not null,
publicationYear varchar2(50) not null,
introduction varchar2(200) not null,
category varchar2(20) not null 
);


4.关掉一些日志:logback.xml

<?xml version="1.0" encoding="UTF-8"?>


<configuration>


  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>


  <root level="INFO">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>


5.实体类:Book.java

package ssh.domain;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;


@Entity // 实体类将映射到数据表,不指定表名即为类名
public class Book {


@Id //定义为主键
@GeneratedValue //JPA通用策略生成器
private Long id;

private String title;
private String author;
private String press;
private String publicationYear;
private String introduction;
private String category;

public Book() {

}


public Book(Long id, String title, String author, String press, String publicationYear, String introduction,
String category) {
super();
this.id = id;
this.title = title;
this.author = author;
this.press = press;
this.publicationYear = publicationYear;
this.introduction = introduction;
this.category = category;
}


public Long getId() {
return id;
}


public void setId(Long id) {
this.id = id;
}


public String getTitle() {
return title;
}


public void setTitle(String title) {
this.title = title;
}


public String getAuthor() {
return author;
}


public void setAuthor(String author) {
this.author = author;
}


public String getPress() {
return press;
}


public void setPress(String press) {
this.press = press;
}


public String getPublicationYear() {
return publicationYear;
}


public void setPublicationYear(String publicationYear) {
this.publicationYear = publicationYear;
}


public String getIntroduction() {
return introduction;
}


public void setIntroduction(String introduction) {
this.introduction = introduction;
}


public String getCategory() {
return category;
}


public void setCategory(String category) {
this.category = category;
}


@Override
public String toString() {
return "Book [id=" + id + ", title=" + title + ", author=" + author + ", press=" + press + ", publicationYear="
+ publicationYear + ", introduction=" + introduction + ", category=" + category + "]";
}



}


6.hibernate.properties

hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.hbm2ddl.auto=create


7.jdbc.properties

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:XE
jdbc.username=mzd
jdbc.password=8341


8.AppConfig.java

package ssh;


import javax.sql.DataSource;


import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;


@Configuration
@ComponentScan("ssh")
@PropertySource("classpath:jdbc.properties") // 引入外部属性文件到Environment
@EnableTransactionManagement // 开启spring事务管理支持
public class AppConfig {


@Bean
public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setPackagesToScan("ssh.domain"); // 自动扫描并注册实体类
return factoryBean;
}

@Bean                       // 依赖Environment
public DataSource dataSource(Environment env) {
DriverManagerDataSource ds = new DriverManagerDataSource();
// env.getProperty("someKey") 获得属性值
ds.setDriverClassName(env.getProperty("jdbc.driverClassName"));
ds.setUrl(env.getProperty("jdbc.url"));
ds.setUsername(env.getProperty("jdbc.username"));
ds.setPassword(env.getProperty("jdbc.password"));
return ds;
}

@Bean
public PlatformTransactionManager transactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory);
return transactionManager;
}

@Bean // 负责将控制器方法返回的字符串映射到某个JSP
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class); // 支持jsp及标准taglib
// customers => /WEB-INF/jsp/customers.jsp
// xyz => /WEB-INF/jsp/ + xyz + .jsp
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}


9.BookController.java

package ssh.controller;




import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import ssh.domain.Book;
import ssh.service.BookService;


@Controller //控制器专用注解
@RequestMapping(value = "/book") //请求地址映射
public class BookController {


@Autowired
private BookService bookServiceImpl;

//保存添加的数据
@RequestMapping(value = "/savebook")
public String create(Book book){
bookServiceImpl.create(book);
return "redirect:main";
}

//跳转到添加页面
@RequestMapping(value = "/addbook")
public String savebook() {
    return "cratepage";
}

// 删除一条数据
@RequestMapping(value = "/delete")
public String delete(@RequestParam(value = "id") Long id) {
System.out.println("删除单个");
bookServiceImpl.delete(id);
return "redirect:main";
}


// 跳转到更新页面,回显数据
@RequestMapping(value = "/doupdate")
public String doupdate(@RequestParam(value = "id") Long id, Model model) {
model.addAttribute("book", bookServiceImpl.find(id));
return "editpage";
}


// 更新数据
@RequestMapping(value = "/updatebook")
public String update(Book book) {
System.out.println(book.toString());
bookServiceImpl.update(book);
return "redirect:main";
}


// 查询所有信息
@RequestMapping(value = "/main")
public String mian(Map<String, Object> map) {
map.put("booklist", bookServiceImpl.findBooks());
return "main";
}
}


10.Dao层

package ssh.dao;


import java.util.List;


import ssh.domain.Book;


public interface BookDao {


    void create(Book book);

void update(Book book);

Book find(Long id);

List<Book> findBooks();

void delete(Long id);
}


package ssh.dao;


import java.util.List;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


import ssh.domain.Book;


@Repository ////Dao专用注解
@Transactional // 拦截该类中所有public方法,应用事务
public class BookDaoImpl implements BookDao {


@Autowired
private SessionFactory sessionFactory;

@Override
public void create(Book book) {


System.out.println("create...");
Session session = sessionFactory.getCurrentSession();
// 增删改查
session.save(book);
}


@Override
public void update(Book book) {


System.out.println("upate...");
Session session = sessionFactory.getCurrentSession();
// 增删改查
session.update(book);
}


@Override
public Book find(Long id) {


System.out.println("find...");
Session session = sessionFactory.getCurrentSession();
// 增删改查
Book book =  session.get(Book.class,id);
return book;
}


@Override
public List<Book> findBooks() {

System.out.println("find All...");
Session session = sessionFactory.getCurrentSession();
// 增删改查
List<Book> booklist =  session.createCriteria(Book.class).list();
return booklist;
}


@Override
public void delete(Long id) {


System.out.println("delete...");
Session session = sessionFactory.getCurrentSession();
// 增删改查
session.delete(session.get(Book.class,id));
}


}


11.Service层

package ssh.service;


import java.util.List;


import ssh.domain.Book;


public interface BookService {


    void create(Book book);

void update(Book book);

Book find(Long id);

List<Book> findBooks();

void delete(Long id);
}


package ssh.service;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import ssh.dao.BookDao;
import ssh.domain.Book;


@Transactional //事务管理
@Service //Service专用注解
public class BookServiceImpl implements BookService {


@Autowired //自动装配,消除 set和get方法.
public BookDao bookDao;

@Override
public void create(Book book) {

bookDao.create(book);
}


@Override
public void update(Book book) {


bookDao.update(book);
}


@Override
public Book find(Long id) {

return bookDao.find(id);
}


@Override
public List<Book> findBooks() {

return bookDao.findBooks();
}


@Override
public void delete(Long id) {

        bookDao.delete(id);     
}


}


12.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextClass</param-name>
      <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>ssh.AppConfig</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>


13.三个JSP页面



14.jar



15.运行截图


项目源码打包下载地址:

http://download.csdn.net/download/mzd8341/10151352

阅读全文
1 0
原创粉丝点击