标签:kill png lap 产生 can let cto 统计 ges
最终效果:
源代码:
1、新建web工程
2、导入jar包
3、配置web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 3 <display-name></display-name> 4 <welcome-file-list> 5 <welcome-file>index.jsp</welcome-file> 6 </welcome-file-list> 7 8 <!-- spring 监听及配置文件路径 --> 9 <listener> 10 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 11 </listener> 12 <context-param> 13 <param-name>contextConfigLocation</param-name> 14 <param-value>classpath:applicationContext.xml</param-value> 15 </context-param> 16 17 <!-- spring MVC配置文件路径 及 配置拦截--> 18 <servlet> 19 <servlet-name>mvc</servlet-name> 20 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 21 <init-param> 22 <param-name>contextConfigLocation</param-name> 23 <param-value>classpath:config/mvc-servlet.xml</param-value> 24 </init-param> 25 </servlet> 26 <servlet-mapping> 27 <servlet-name>mvc</servlet-name> 28 <url-pattern>*.do</url-pattern> 29 </servlet-mapping> 30 31 32 <!-- 编码配置 --> 33 <filter> 34 <filter-name>encodingFilter</filter-name> 35 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 36 <init-param> 37 <param-name>encoding</param-name> 38 <param-value>utf-8</param-value> 39 </init-param> 40 </filter> 41 <filter-mapping> 42 <filter-name>encodingFilter</filter-name> 43 <url-pattern>/*</url-pattern> 44 </filter-mapping> 45 46 47 </web-app>
4、配置mvc-servlet.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 3 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 4 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 5 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 6 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 7 8 9 <!-- 让spring 去扫描类 建立关联 --> 10 <!-- 是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用 --> 11 <mvc:annotation-driven /> 12 13 14 <!-- 扫描controller包即下面的控制器 --> 15 <context:component-scan base-package="com.hw.controller"></context:component-scan> 16 17 18 <!-- 试图解析器 --> 19 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 20 <!-- 前缀 --> 21 <property name="prefix" value="/WEB-INF/per/"></property> 22 <!-- 后缀 --> 23 <property name="suffix" value=".jsp"></property> 24 </bean> 25 26 27 <!-- 文件上传解析器 --> 28 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 29 <!-- one of the properties available; the maximum file size in bytes --> 30 <property name="defaultEncoding" value="utf-8" /> 31 <property name="maxUploadSize" value="104857600" /> 32 <property name="maxInMemorySize" value="4096" /> 33 </bean> 34 </beans>
5、配置applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 3 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 4 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 5 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 6 7 <!-- 自动扫描 --> 8 <context:component-scan base-package="com.hw"></context:component-scan> 9 10 11 <!-- 加载属性文件 --> 12 <context:property-placeholder location="classpath:jdbc.properties" /> 13 14 15 <!-- 配置c3p0 --> 16 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 17 <property name="driverClassName"> 18 <value>${jdbc.driverClassName}</value> 19 </property> 20 <property name="url"> 21 <value>${jdbc.url}</value> 22 </property> 23 <property name="username"> 24 <value>${jdbc.username}</value> 25 </property> 26 <property name="password"> 27 <value>${jdbc.password}</value> 28 </property> 29 <!--连接池中保留的最小连接数。 --> 30 <property name="initialSize" value="3" /> 31 <!-- 连接池的最大值 --> 32 <property name="maxActive" value="300" /> 33 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> 34 <property name="maxIdle" value="2" /> 35 <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> 36 <property name="minIdle" value="1" /> 37 </bean> 38 39 40 <!--配置 mybaties --> 41 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 42 <!--dataSource属性指定要用到的连接池 --> 43 <property name="dataSource" ref="dataSource" /> 44 <!-- 所有配置的mapper文件 --> 45 <property name="mapperLocations" value="classpath*:com/hw/mapper/*Mapper.xml" /> 46 </bean> 47 48 49 <!-- 配置bean 自动扫描所有mapper 自动给Mapper接口产生代理类对象 并且给代理对象注入SqlSessionFactory --> 50 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 51 <property name="basePackage"> 52 <value>com.hw.mapper</value><!-- 即原来框架整合时的dao包 --> 53 </property> 54 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> 55 </bean> 56 57 58 <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> 59 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 60 <property name="dataSource" ref="dataSource" /> 61 </bean> 62 63 64 <!-- 纯注解方式,只需要所需类前加上 @Transactional --> 65 <tx:annotation-driven transaction-manager="transactionManager" /> 66 </beans>
6、配置jdbc.properties
1 jdbc.driverClassName=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/cc3?useUnicode=true&characterEncoding=utf8 3 jdbc.username=root 4 jdbc.password=root
7、com.hw.entity中的代码:
1)Person.java中代码:
1 package com.hw.entity; 2 3 import java.util.Date; 4 5 public class Person { 6 private int pid; 7 private String pname; 8 private String psex; 9 private String skilled; 10 private String degree; 11 private Date jobtime;// 要和数据库中表的字段类型一致 12 private String resume; 13 private String filepath;// 上传图片,名要为filepath 14 private Dept dept; 15 16 public int getPid() { 17 return pid; 18 } 19 20 public void setPid(int pid) { 21 this.pid = pid; 22 } 23 24 public String getPname() { 25 return pname; 26 } 27 28 public void setPname(String pname) { 29 this.pname = pname; 30 } 31 32 public String getPsex() { 33 return psex; 34 } 35 36 public void setPsex(String psex) { 37 this.psex = psex; 38 } 39 40 public String getSkilled() { 41 return skilled; 42 } 43 44 public void setSkilled(String skilled) { 45 this.skilled = skilled; 46 } 47 48 public String getDegree() { 49 return degree; 50 } 51 52 public void setDegree(String degree) { 53 this.degree = degree; 54 } 55 56 public Date getJobtime() { 57 return jobtime; 58 } 59 60 public void setJobtime(Date jobtime) { 61 this.jobtime = jobtime; 62 } 63 64 public String getResume() { 65 return resume; 66 } 67 68 public void setResume(String resume) { 69 this.resume = resume; 70 } 71 72 public String getFilepath() { 73 return filepath; 74 } 75 76 public void setFilepath(String filepath) { 77 this.filepath = filepath; 78 } 79 80 public Dept getDept() { 81 return dept; 82 } 83 84 public void setDept(Dept dept) { 85 this.dept = dept; 86 } 87 88 public Person(int pid, String pname, String psex, String skilled, 89 String degree, Date jobtime, String resume, String filepath, 90 Dept dept) { 91 super(); 92 this.pid = pid; 93 this.pname = pname; 94 this.psex = psex; 95 this.skilled = skilled; 96 this.degree = degree; 97 this.jobtime = jobtime; 98 this.resume = resume; 99 this.filepath = filepath; 100 this.dept = dept; 101 } 102 103 public Person(String pname, String psex, String skilled, String degree, 104 Date jobtime, String resume, String filepath, Dept dept) { 105 super(); 106 this.pname = pname; 107 this.psex = psex; 108 this.skilled = skilled; 109 this.degree = degree; 110 this.jobtime = jobtime; 111 this.resume = resume; 112 this.filepath = filepath; 113 this.dept = dept; 114 } 115 116 public Person() { 117 super(); 118 } 119 120 }
2) Dept.java中代码:
1 package com.hw.entity; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import org.hibernate.annotations.Entity; 7 8 import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 9 public class Dept implements java.io.Serializable{ 10 private int did; 11 private String dname; 12 private Set<Person> per = new HashSet<Person>(); 13 //都有set get方法,全参、无参、除id外的构造方法 14 public Dept() { 15 super(); 16 } 17 18 public Dept(String dname, Set<Person> per) { 19 super(); 20 this.dname = dname; 21 this.per = per; 22 } 23 24 public Dept(int did, String dname, Set<Person> per) { 25 super(); 26 this.did = did; 27 this.dname = dname; 28 this.per = per; 29 } 30 31 public int getDid() { 32 return did; 33 } 34 35 public void setDid(int did) { 36 this.did = did; 37 } 38 39 public String getDname() { 40 return dname; 41 } 42 43 public void setDname(String dname) { 44 this.dname = dname; 45 } 46 47 public Set<Person> getPer() { 48 return per; 49 } 50 51 public void setPer(Set<Person> per) { 52 this.per = per; 53 } 54 55 }
3) User.java中代码:
1 package com.hw.entity; 2 3 import com.hw.utils.HibernateXMLAutoCreateUtils; 4 5 public class User { 6 private int uid; 7 private String userName, userPass; 8 9 public int getUid() { 10 return uid; 11 } 12 13 public void setUid(int uid) { 14 this.uid = uid; 15 } 16 17 public String getUserName() { 18 return userName; 19 } 20 21 public void setUserName(String userName) { 22 this.userName = userName; 23 } 24 25 public String getUserPass() { 26 return userPass; 27 } 28 29 public void setUserPass(String userPass) { 30 this.userPass = userPass; 31 } 32 33 public User(int uid, String userName, String userPass) { 34 super(); 35 this.uid = uid; 36 this.userName = userName; 37 this.userPass = userPass; 38 } 39 40 public User(String userName, String userPass) { 41 super(); 42 this.userName = userName; 43 this.userPass = userPass; 44 } 45 46 public User() { 47 super(); 48 } 49 public static void main(String[] args) { 50 HibernateXMLAutoCreateUtils.createHibernatePOJOXML(User.class, "uid", "tab_user"); 51 } 52 }
4) Test.java中代码:
1 package com.hw.entity; 2 3 import com.hw.utils.HibernateXMLAutoCreateUtils; 4 5 public class Test { 6 public static void main(String[] args) { 7 HibernateXMLAutoCreateUtils.createHibernatePOJOMappingManyToOne(Person.class, "pid", "tab_per", Dept.class, "did", "tab_dept"); 8 } 9 }
8、com.hw.mapper中的代码:
1)DeptDao.java中的代码:
1 package com.hw.mapper; 2 3 import java.util.List; 4 5 import com.hw.entity.Dept; 6 7 public interface DeptDao { 8 public void add(Dept dept); 9 10 public List<Dept> list(); 11 }
2)PersonDao.java中的代码:
1 package com.hw.mapper; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import com.hw.entity.Person; 7 8 public interface PersonDao { 9 public int getCount();// 统计总记录 10 11 public void add(Person per);// 添加 12 13 public void updatePerson(Person per);// 修改 14 15 public void del(int id);// 删除 16 17 public void delSelectAll(String id[]);// 批量删除,要用数组 18 19 public Person getPerson(int id); 20 21 public List<Person> list(Map<String, Object> map);// 查询有分页 22 23 public List<Person> listLike(Map<String, Object> map);// 模糊查询有分页 24 25 public int getLikeCount(Map<String, Object> map); 26 27 public List<Person> listAll();// 查询所有有 28 }
3) UserDao.java中的代码:
1 package com.hw.mapper; 2 3 import java.util.Map; 4 5 import com.hw.entity.User; 6 7 public interface UserDao { 8 void add(User user);// 用户注册 9 10 boolean login(Map<String, Object> map);// 用户登录 11 }
4) deptDaoMapper.xml中的代码:
1 <?xml version="1.0" encoding="UTF-8" ?><!-- 相当于实现dao中的接口 --> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!-- namespace如果和spring整合时要用接口全类名,不整合任意名,id要用接口中的方法名 --> 6 <mapper namespace="com.hw.mapper.DeptDao"> 7 8 <resultMap type="com.hw.entity.Dept" id="deptinfo"><!-- 如果不用resultMap则不写 --> 9 <result column="did" property="did" /> 10 <result column="dname" property="dname" /> 11 12 13 <!-- mybatis中 1方配置多方 --> 14 <collection property="per" ofType="com.hw.entity.Person"> 15 <result column="pid" property="pid" /> 16 <result column="pname" property="pname" /> 17 <result column="psex" property="psex" /> 18 <result column="skilled" property="skilled" /> 19 <result column="degree" property="degree" /> 20 <result column="jobtime" property="jobtime" javaType="java.sql.Date" jdbcType="DATE" /> 21 <result column="resume" property="resume" /> 22 <result column="filepath" property="filepath" /> 23 </collection> 24 </resultMap> 25 26 27 <!-- parameterType参数类型,parameterMap参数集合,id要用接口中的方法名 --> 28 <select id="list" resultMap="deptinfo"><!-- 用户登录 --> 29 select * from tab_dept 30 </select> 31 32 33 <insert id="add" parameterType="com.hw.entity.Dept"> 34 insert into tab_dept values(null,#{dname}) 35 </insert> 36 37 </mapper>
5) personDaoMapper.xml中的代码:
20170314_ssm(spring+spring MVC +Mybatis)
标签:kill png lap 产生 can let cto 统计 ges
原文地址:http://www.cnblogs.com/yhmdjl/p/6549910.html