spring+hirbernate+springmvc+oracle

spring整合hiebernate.

意义:只需编辑dao层,就可以映射到数据库,无需编辑sql.

第一步:添加spring核心包,

添加hibernate包 E:\hibernate-release-5.0.7.Final\lib\required

添加c3p0整合hibernate包 E:\hibernate-release-5.0.7.Final\lib\optional\c3p0

添加oracle驱动包

第二步:编写实体类:

package com.mobile263.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;


@Entity(name="t_customer")
public class Customer implements Serializable{
	
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "customer_seq")
	@SequenceGenerator(initialValue = 1, name = "customer_seq", sequenceName = "seq_customer")
	@Column(name="cust_id")
	 private Long  cutId;
	
	@Column(name="cust_name")
	 private String custName;
	
	@Column(name="cust_telephone")
	 private String custTelephone;

	public Long getCutId() {
		return cutId;
	}

	public void setCutId(Long cutId) {
		this.cutId = cutId;
	}

	public String getCustName() {
		return custName;
	}

	public void setCustName(String custName) {
		this.custName = custName;
	}

	public String getCustTelephone() {
		return custTelephone;
	}

	public void setCustTelephone(String custTelephone) {
		this.custTelephone = custTelephone;
	}
	 
	
}

第三步:配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:contenxt="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
       	http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        
        <contenxt:property-placeholder location="classpath:jdbc.properties"/>
        
        <!-- create c3po -->
		<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
			<property name="jdbcUrl" value="${jdbc.url}"></property>
			<property name="driverClass"  value="${jdbc.driver_class}"></property>
			<property name="user"  value="${jdbc.user}"></property>
			<property name="password" value="${jdbc.password}"></property>
		
		</bean>
		
		<!-- spring 整合 Hibernate -->
		<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
			<property name="dataSource" ref="dataSource"></property>
			<property name="hibernateProperties">
				<props>
					<prop key="hibernate.show_sql">true</prop>
					<prop key="hibernate.hbm2ddl.auto">update</prop>
				</props>
			</property>
			
			<property name="packagesToScan">
				<list>
					<value>com.mobile263.domain</value>
				</list>
			</property>
			
		</bean>
		
		<!-- 开启spring的事务管理 -->
		<tx:annotation-driven transaction-manager="transactionManager"/>
		<bean id="transactionManager"
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
			<property name="sessionFactory" ref="sessionFactory"></property>
		</bean>
		
		<contenxt:component-scan base-package="com.mobile263.Dao,com.mobile263.Dao,com.mobile263.service"></contenxt:component-scan>
	
</beans>

第四步:编写dao接口和实现

package com.mobile263.Dao;


import java.util.List;

import com.mobile263.domain.Customer;


public interface CustomerDao {
	public void save(Customer customer);
	public List<Customer> findAll();
	public Customer findById(Long id);
	public void update(Customer customer);
	public void delete(Long[] cutIds);
}


package com.mobile263.Dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.mobile263.domain.Customer;

@Repository
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

	@Resource
	public void setMySessinFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
	@Override
	public void save(Customer customer) {
		this.getHibernateTemplate().save(customer);
	}

	@Override
	public List<Customer> findAll() {
		return this.getHibernateTemplate().loadAll(Customer.class);
	}

	@Override
	public Customer findById(Long id) {

		return this.getHibernateTemplate().get(Customer.class, id);
	}

	@Override
	public void update(Customer customer) {
		
		this.getHibernateTemplate().update(customer);
	}

	@Override
	public void delete(Long[] cutIds) {

		for (Long cutId : cutIds) {
			this.getHibernateTemplate().delete(findById(cutId));
		}
		
	}
}

编写service层和实现:

package com.mobile263.service;

import java.util.List;

import com.mobile263.domain.Customer;

public interface CustomerService {

	public void save(Customer customer);
	public List<Customer> findAll();
	public Customer findById(Long id);
	public void update(Customer customer);
	public void delete(Long[] cutIds);
}


package com.mobile263.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mobile263.Dao.CustomerDao;
import com.mobile263.domain.Customer;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {

	@Resource
	private CustomerDao customerDao;
	
	@Override
	public void save(Customer customer) {
	
		customerDao.save(customer);
	}

	@Override
	public List<Customer> findAll() {
		return customerDao.findAll();
	}

	@Override
	public Customer findById(Long id) {

		return customerDao.findById(id);
	}

	@Override
	public void update(Customer customer) {
		
		customerDao.update(customer);
	}

	@Override
	public void delete(Long[] cutIds) {
		
		customerDao.delete(cutIds);
	}


}

第五步:整合spring-mvc

添加springmvc包。

第六步:编写web.xml,

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
	<display-name></display-name>
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<filter>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
	

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

第七步:编写spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:contenxt="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
       	http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
	
	<contenxt:component-scan base-package="com.mobile263.controller"></contenxt:component-scan>
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
		
	
</beans>

第八步,编写controller

package com.mobile263.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mobile263.domain.Customer;
import com.mobile263.service.CustomerService;


@Controller
@RequestMapping("/customer")
public class CustomerController {

	@Resource
	private CustomerService  customerService;
	
	@RequestMapping("/saveUi")
	public String saveUi(){
		return "save";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String save(Customer customer,Map<String, Object> modMap){
		System.out.println("CustomerController.save");
		customerService.save(customer);
		modMap.put("msg", "添加成功");
		return "success";
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public String findAll(Map<String, Object> modelMap){
		System.out.println("CustomerController.findAll");
		List<Customer> cList = customerService.findAll();
		modelMap.put("cList", cList);
		return "list";
	}
	
	@RequestMapping(value="/{id}", method=RequestMethod.GET)
	public String findById(@PathVariable("id") Long id, Map<String, Object> modelMap){
		System.out.println("CustomerController.findById");
		Customer customer = customerService.findById(id);
		modelMap.put("customer", customer);
		return "edit";
	}

	@RequestMapping(method=RequestMethod.PUT)
	public String update(Customer customer, Map<String, Object> modelMap){
		System.out.println("CustomerController.update");
		customerService.update(customer);
		List<Customer> cList = customerService.findAll();
		modelMap.put("cList", cList);
		return "list";
	}
	
	
	@RequestMapping(method=RequestMethod.DELETE)
	public String delete(Long[] cutids, Map<String, Object> modelMap){
		System.out.println("CustomerController.delete");
		customerService.delete(cutids);
		List<Customer> cList = customerService.findAll();
		modelMap.put("cList", cList);
		return "list";
	}

}

最后一步:编写自己页面jsp。

最后我的启动界面如下:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值