一下是习惯的xml配置跟Java代码
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
myshop
<welcome-file>customer_login.jsp</welcome-file>
contextConfigLocation
classpath:config/springAnnotation-.xml
org.springframework.web.context.ContextLoaderListener
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:config/springAnnotation-servlet.xml
1
springMVC
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
openSession
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
openSession
/*
关于hibernate的配置
<?xml version="1.0" encoding="UTF-8"?>
]>
org.hibernate.dialect.MySQLDialect
create
true
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_NEVER
关于spring的配置
<?xml version="1.0" encoding="UTF-8"?>
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<property name="defaultEncoding" value="utf-8"/>
<!-- <property name="maxUploadSize" value="10485760000"/>
<property name="maxInMemorySize" value="40960"/>
-->
</bean>
Java代码实体类
package cn.zmf.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="t_customer")
public class Customer {
@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid",strategy="uuid")
@Column(length=36)
private String customerId;
@Column(length=32)
private String customerName;
@Column(length=32)
private String customerSex;
@Column(length=32)
private String customerAge;
@Column(length=32)
private String customerPassword1;
@Column(length=32)
private String customerPassword2;
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerSex() {
return customerSex;
}
public void setCustomerSex(String customerSex) {
this.customerSex = customerSex;
}
public String getCustomerAge() {
return customerAge;
}
public void setCustomerAge(String customerAge) {
this.customerAge = customerAge;
}
public String getCustomerPassword1() {
return customerPassword1;
}
public void setCustomerPassword1(String customerPassword1) {
this.customerPassword1 = customerPassword1;
}
public String getCustomerPassword2() {
return customerPassword2;
}
public void setCustomerPassword2(String customerPassword2) {
this.customerPassword2 = customerPassword2;
}
}
Java代码Dao
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.stereotype.Service;
import cn.zmf.entity.Customer;
@Service(value="customerLoginDao")
public class CustomerLoginDao implements ICustomerLoginDao {
@Resource(name="sessionFactory")
private SessionFactory sessionFactory;
@Override
public void register(Customer customer) {
try {
Session session = sessionFactory.getCurrentSession();
session.save(customer);
session.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
service代码
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.zmf.dao.CustomerLoginDao;
import cn.zmf.entity.Customer;
@Service(value="customerLoginService")
public class CustomerLoginService implements ICustomerLoginService {
@Resource(name="customerLoginDao")
private CustomerLoginDao customerLoginDao;
@Override
public void register(Customer customer) {
customerLoginDao.register(customer);
}
}
controller代码
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.zmf.entity.Customer;
import cn.zmf.service.CustomerLoginService;
@Controller
@RequestMapping("/customer")
public class CustomerLoginController {
@Resource(name="customerLoginService")
private CustomerLoginService customerLoginService;
@RequestMapping("/register")
public String register(Customer customer){
customerLoginService.register(customer);
return "/customerregister";
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。