赞
踩
- 一、项目初始化
- ------------------------------------------------------------
- 1.IDEA配置tomcat服务器
- idea - > settings -> application servers --> + --> tomcat安装目录.
-
- 2.创建idea的新工程EShop
-
- 3.创建web模块,添加web application支持和maven支持和Spring4的支持
-
- 4.运行配置
- run菜单->edit configuration->添加tomcat server-->右侧编辑名字,deployment添加模块
-
- 5.创建包结构
- com.test.eshop.model
- com.test.eshop.dao
- com.test.eshop.dao.impl
- com.test.eshop.service
- com.test.eshop.service.impl
- com.test.eshop.util
- com.test.eshop.web
- com.test.eshop.web.controller
-
- 6.添加junit和c3p0数据源 maven支持
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- </dependency>
- <dependency>
- <groupId>c3p0</groupId>
- <artifactId>c3p0</artifactId>
- <version>0.9.1.2</version>
- </dependency>
-
- 7.准备win7下的mysql数据库
- a.创建数据库eshop
- 登录mysql
- $mysql> create database eshop ;
-
- b.创建users表
- $mysql> create table eshop.users(id int primary key auto_increment,name varchar(20),password varchar(20),regdate datetime) ;
-
- c.显式表结构
- $mysql> desc users;
- +----------+-------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+-------------+------+-----+---------+----------------+
- | id | int(11) | NO | PRI | NULL | auto_increment |
- | name | varchar(20) | YES | | NULL | |
- | password | varchar(20) | YES | | NULL | |
- | regdate | datetime | YES | | NULL | |
- +----------+-------------+------+-----+---------+----------------+
-
- 8.创建用户类
- package com.test.eshop.model;
- import java.sql.Date;
- /**
- * User类
- */
- public class User {
- private Integer id ;
- private String name ;
- private String password ;
- private Date regDate ;
- //getXxx/setXxx
- }
-
- 9.引入新的maven依赖,hibernate
- [pom.xml]
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-core</artifactId>
- <version>3.5.6-Final</version>
- </dependency>
-
- 10.编写User类和users表之间的映射文件User.hbm.xml和User类放在一个目录下.
- [User.hbm.xml] //hibernate mapping
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.it18zhang.eshop.model.User" table="users">
- <id name="id" column="id" type="integer">
- <generator class="identity" />
- </id>
- <property name="name" column="name" type="string" />
- <property name="password" column="password" type="string" />
- <property name="regDate" column="regdate" type="date" />
- </class>
- </hibernate-mapping>
-
- 15.引入springframework依赖
- [pom.xml]
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>4.3.3.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>4.3.3.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-orm</artifactId>
- <version>4.3.3.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-hibernate</artifactId>
- <version>RELEASE</version>
- </dependency>
-
- 16.引入mysql的驱动
- [pom.xml]
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.17</version>
- </dependency>
-
- 17.resouces目录下创建jdbc配置文件jdbc.properties
- jdbc.driverclass=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://192.168.43.1:3306/eshop
- jdbc.username=mysql
- jdbc.password=mysql
- c3p0.pool.size.max=10
- c3p0.pool.size.min=2
- c3p0.pool.size.ini=3
- c3p0.pool.size.increment=2
-
- 18.创建spring的配置文件[resources/beans.xml]
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- 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">
-
- <!-- 指定外部文件的位置 -->
- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location" value="classpath:jdbc.properties" />
- </bean>
-
- <!-- c3p0数据源 -->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="${jdbc.driverclass}" />
- <property name="jdbcUrl" value="${jdbc.url}" />
- <property name="user" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
-
- <property name="maxPoolSize" value="${c3p0.pool.size.max}"/>
- <property name="minPoolSize" value="${c3p0.pool.size.min}"/>
- <property name="initialPoolSize" value="${c3p0.pool.size.ini}"/>
- <property name="acquireIncrement" value="${c3p0.pool.size.increment}"/>
- </bean>
- </beans>
-
- 19.测试数据源类
- package com.it18zhang.eshop.test;
-
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import javax.sql.DataSource;
- import java.sql.Connection;
-
- /**
- * 测试数据源
- */
- public class TestDataSource {
- /**
- * 测试连接是否成功
- */
- @Test
- public void getConn() throws Exception {
- ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
- DataSource ds = (DataSource) ac.getBean("dataSource");
- Connection conn = ds.getConnection() ;
- System.out.println(conn);
- }
- }
-
-
- 二、创建Dao接口及其实现类
- -------------------------------------------------------
- 1.创建BaseDao接口
- Dao : data access Object.数据访问对象.
- package com.test.eshop.dao;
-
- import java.util.List;
-
- /**
- * BaseDao接口
- */
- public interface BaseDao<T> {
-
- public void saveEntity(T t);
- public void updateEntity(T t);
- public void saveOrUpdateEntity(T t);
- public void deleteEntity(T t);
- public T getEntity(Integer id);
-
- /* 执行HSQL*/
- public List<T> findByHQL(String hql,Object ... objects);
- public void execHQL(String hql, Object ... objects);
-
- }
-
- 2.编写BaseDao的实现类BaseDaoImpl<T>
- package com.test.eshop.dao.impl;
-
- import com.test.eshop.dao.BaseDao;
- import org.hibernate.Query;
- import org.hibernate.SessionFactory;
- import java.lang.reflect.ParameterizedType;
- import java.util.List;
-
- /**
- * 基本Dao的实现类
- */
- public abstract class BaseDaoImpl<T> implements BaseDao<T> {
-
- //引入hibernate的会话工厂--连接池
- private SessionFactory sf;
-
- //设置会话工厂
- public void setSf(SessionFactory sf) {
- this.sf = sf;
- }
-
- //用来加载通配的类的描述符
- private Class<T> clazz;
-
- public BaseDaoImpl()
- {
- //得到泛型化超类
- ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();
- //得到泛型T的实际类
- clazz = (Class)type.getActualTypeArguments()[0];
- }
-
-
-
- public void saveEntity(T t) {
- sf.getCurrentSession().save(t);
- }
-
- public void updateEntity(T t) {
- sf.getCurrentSession().update(t);
- }
-
- public void saveOrUpdateEntity(T t) {
- sf.getCurrentSession().saveOrUpdate(t);
- }
-
- public void deleteEntity(T t) {
- sf.getCurrentSession().delete(t);
- }
-
- public T getEntity(Integer id) {
- return (T)sf.getCurrentSession().get(clazz,id);
- }
-
- /**
- * 使用hivesql语句查询操作
- */
- public List<T> findByHQL(String hql, Object... objects) {
-
- Query q = sf.getCurrentSession().createQuery(hql);
- //绑定参数
- for (int i = 0; i < objects.length; i++) {
- q.setParameter(i, objects[i]);
- }
-
- return q.list();
- }
-
- /**
- * 使用hivesql语句批量写操作
- */
- public void execHQL(String hql, Object... objects) {
- Query q = sf.getCurrentSession().createQuery(hql);
- //绑定参数
- for (int i = 0; i < objects.length; i++) {
- q.setParameter(i, objects[i]);
- }
- //执行sql
- q.executeUpdate();
- }
- }
-
- 3.创建用户的Dao实现类
- package com.test.eshop.dao.impl;
-
- import com.test.eshop.model.User;
-
- /**
- * 具体用户Dao实现类 -- 处理用户表的类
- */
- public class UserDaoImpl extends BaseDaoImpl<User> {
-
-
-
- }
-
-
- 三、创建基本服务接口和实现类 -- 交互dao的服务
- -----------------------------------------------------
- 1.创建基本服务接口BaseService<T>.java
- package com.test.eshop.service;
-
- import java.util.List;
-
- /**
- * BaseService接口.
- */
- public interface BaseService<T> {
- public void saveEntity(T t);
-
- public void updateEntity(T t);
-
- public void saveOrUpdateEntity(T t);
-
- public void deleteEntity(T t);
-
- public T getEntity(Integer id);
-
- /* 按照HQL */
- public List<T> findByHQL(String hql, Object... objects);
-
- public void execHQL(String hql, Object... objects);
- }
-
- 2.创建基本服务的基本实现类
- package com.test.eshop.service.impl;
-
- import com.test.eshop.dao.BaseDao;
- import com.test.eshop.service.BaseService;
-
- import java.util.List;
-
- /**
- * BaseSercie接口的实现类
- */
- public abstract class BaseServiceImpl<T> implements BaseService<T> {
-
- private BaseDao<T> dao;
-
- public BaseDao<T> getDao() {
- return dao;
- }
-
- public void setDao(BaseDao<T> dao) {
- this.dao = dao;
- }
-
- public void saveEntity(T t) {
- dao.saveEntity(t);
- }
-
- public void updateEntity(T t) {
- dao.updateEntity(t);
- }
-
- public void saveOrUpdateEntity(T t) {
- dao.saveOrUpdateEntity(t);
- }
-
- public void deleteEntity(T t) {
- dao.deleteEntity(t);
- }
-
- public T getEntity(Integer id) {
- return dao.getEntity(id);
- }
-
- public List<T> findByHQL(String hql, Object... objects) {
- return dao.findByHQL(hql,objects);
- }
-
- public void execHQL(String hql, Object... objects) {
- dao.execHQL(hql,objects);
- }
- }
-
-
-
-
- 3.创建用户Service接口
- package com.test.eshop.service;
-
- import com.test.eshop.model.User;
-
- /**
- * 用户独有的service接口
- */
- public interface UserService<User> extends BaseService<User>{
-
-
- }
-
- 4.创建用户Service接口的实现类
- package com.test.eshop.service.impl;
-
- import com.test.eshop.model.User;
- import com.test.eshop.service.UserService;
-
- public class UserServiceImpl<User> extends BaseServiceImpl<User> implements UserService<User> {
-
- }
-
-
- 四、引入Spring MVC框架
- ----------------------------------------------
- 1.pom.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>com.test</groupId>
- <artifactId>eshop</artifactId>
- <version>1.0-SNAPSHOT</version>
-
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- </dependency>
- <dependency>
- <groupId>c3p0</groupId>
- <artifactId>c3p0</artifactId>
- <version>0.9.1.2</version>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-core</artifactId>
- <version>4.3.8.Final</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>4.3.3.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>4.3.3.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-orm</artifactId>
- <version>4.3.3.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>4.3.3.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>4.3.3.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.aspectj</groupId>
- <artifactId>aspectjweaver</artifactId>
- <version>1.8.10</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.17</version>
- </dependency>
-
- </dependencies>
-
- </project>
-
-
- 2.配置web-inf/web.xml
- [web/WEB-INF/web.xml]
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
- version="3.1">
-
- <!-- 上下文配置参数,指定的是spring的beans.xml文件位置-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:beans.xml</param-value>
- </context-param>
- <!--通过监听器方式在tomcat启动时,完成beans容器的初始化-->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
-
- <!-- 配置Spring的分发器控制器-->
- <servlet>
- <servlet-name>controller</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <!-- 初始化参数,确保web服务器启动时,初始化springmvc容器 -->
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/webmvc.xml</param-value>
- </init-param>
- </servlet>
- <!-- 映射 -->
- <servlet-mapping>
- <servlet-name>controller</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
-
- 3.创建resources/webmvc.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:context="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-4.3.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.3.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
- <mvc:default-servlet-handler />
- <mvc:annotation-driven />
- <!-- 扫描controller包 -->
- <context:component-scan base-package="com.test.eshop.web.controller" />
-
- <!-- 内部资源视图解析器(解析的是jsp的路径) -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/jsps/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
- </beans>
-
-
- 4.给类添加注解
- a.注入会话工厂@Resource(name = "sf")
- public abstract class BaseDaoImpl<T> implements BaseDao<T> {
-
- //引入hibernate的会话工厂--连接池
- private SessionFactory sf;
-
-
- //设置会话工厂
- @Resource(name = "sf")
- public void setSf(SessionFactory sf) {
- this.sf = sf;
- }
-
- b.注册userDaoImpl到bean仓库中 @Repository("userDao")
- @Repository("userDao")
- public class UserDaoImpl extends BaseDaoImpl<User> {
-
- }
-
- c.注册服务UserServiceImpl到仓库中。并且注入userDao
- @Service("userService")
- public class UserServiceImpl<User> extends BaseServiceImpl<User> implements UserService<User> {
-
- /**
- * 重新该方法,需要注入指定的UserDao对象
- */
- @Resource(name = "userDao")
- public void setDao(BaseDao<User> dao) {
- super.setDao(dao);
- }
- }
-
- 5.测试Spring框架是否引入成功
- //测试用户Service
- @Test
- public void teUserService()
- {
- ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
- UserService us = (UserService) ac.getBean("userService");
- User u = new User();
- u.setName("tom");
- u.setPassword("123456");
- us.saveEntity(u);
- }
-
- 6.编写控制器HomeController
- package com.test.eshop.web.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
-
- /**
- * 主页控制器
- */
- @Controller
- public class HomeController {
-
- /**
- * 去主页
- */
- @RequestMapping(value = "/home", method = RequestMethod.GET)
- public String toHomePage()
- {
- return "index";
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。