必须明本文章==》http://www.cnblogs.com/zhu520/p/7883273.html

一:

1):我的运行环境

我使用myeclipse(你也可以使用eclipse),tomcat7

jar包 放在百度云,托到文章最后有链接下载即可(其实也可以根据我之前http://www.cnblogs.com/zhu520/p/7772823.html 去弄,不需要去网上下载(但是只是对myeclipse而言,eclipse还是要到网上下载的jar包的))

可以去看看孤傲苍狼写的Mybatis  https://www.cnblogs.com/xdp-gacl/p/4261895.html

2):包的情况

3):配置

applicationContext.xml ==》少了很多代码

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:aop="http://www.springframework.org/schema/aop"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  14. http://www.springframework.org/schema/context
  15. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  16. http://www.springframework.org/schema/beans
  17. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
  18. <!-- 开启注解 -->
  19. <context:annotation-config />
  20. <!-- 自动扫描 -->
  21. <context:component-scan base-package="zhu.dao,zhu.service">
  22. <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
  23. <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
  24. </context:component-scan>
  25. </beans>

jdbc.properties

  1. driverClassName=com.mysql.jdbc.Driver
  2. url=jdbc\:mysql\://localhost\:3306/jdbc01?useUnicode\=true&amp;characterEncoding\=utf-8
  3. username=root
  4. password=root
  5. validationQuery=SELECT 1
  6. # \u6570\u636e\u5e93\u914d\u7f6e
  7. jdbc.driverClass=com.mysql.jdbc.Driver
  8. jdbc.url=jdbc\:mysql\://localhost\:3306/user?useUnicode\=true&amp;characterEncoding\=utf-8&zeroDateTimeBehavior\=convertToNull
  9. jdbc.user=root
  10. jdbc.password=root
  11. #\u5305\u6587\u4ef6\u4f4d\u7f6e
  12. targetProject=THIS4
  13. package.model=zhu.po
  14. package.sql.mapper=zhu.mapper
  15. package.dao.mapper=zhu.dao

jdbc.properties

spring-mvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  9. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
  11.  
  12. <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
  13. <context:component-scan base-package="zhu.web">
  14. <context:include-filter type="annotation"
  15. expression="org.springframework.stereotype.Controller" />
  16. </context:component-scan>
  17.  
  18. <!-- 日期转换 必须放在<mvc:annotation-driven />前面 -->
  19. <bean
  20. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
  21.  
  22. <!-- 注解方式 -->
  23. <mvc:annotation-driven>
  24. </mvc:annotation-driven>
  25.  
  26. <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
  27. <bean
  28. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
  29.  
  30. <!--mvc配置视图解析 -->
  31. <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
  32. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
  33. <property name="prefix" value="/WEB-INF/"></property>
  34. <property name="suffix" value=".jsp"></property>
  35. </bean>
  36. <!--跳转的时候只用写jsp名字,不用带后缀,因为默认的后缀“.jsp”,路径为“/WEB-INF/jsp” -->
  37.  
  38. </beans>

spring-mybatis.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/tx
  8. http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  9. http://www.springframework.org/schema/aop
  10. http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  13. http://www.springframework.org/schema/beans
  14. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
  15.  
  16. <!-- ========================================数据库方言========================================= -->
  17. <!-- properties配置文件 -->
  18. <bean id="dbProperties"
  19. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  20. <!-- 是否忽略不可解析的 -->
  21. <property name="ignoreUnresolvablePlaceholders" value="true" />
  22. <property name="locations">
  23. <list>
  24. <value>classpath:jdbc.properties</value>
  25. </list>
  26. </property>
  27. </bean>
  28.  
  29. <!-- ========================================数据源配置========================================= -->
  30. <!-- 配置数据源,使用的是alibaba的Druid(德鲁伊)数据源 -->
  31. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
  32. init-method="init" destroy-method="close">
  33. <property name="url" value="${url}" />
  34. <property name="username" value="${username}" />
  35. <property name="password" value="${password}" />
  36. <!-- 初始化连接大小 -->
  37. <property name="initialSize" value="0" />
  38. <!-- 连接池最大使用连接数量 -->
  39. <property name="maxActive" value="20" />
  40. <!-- 连接池最大空闲 -->
  41. <property name="maxIdle" value="20" />
  42. <!-- 连接池最小空闲 -->
  43. <property name="minIdle" value="0" />
  44. <!-- 获取连接最大等待时间 -->
  45. <property name="maxWait" value="60000" />
  46. <property name="validationQuery" value="${validationQuery}" />
  47. <property name="testOnBorrow" value="false" />
  48. <property name="testOnReturn" value="false" />
  49. <property name="testWhileIdle" value="true" />
  50. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  51. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  52. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  53. <property name="minEvictableIdleTimeMillis" value="25200000" />
  54. <!-- 打开removeAbandoned功能 -->
  55. <property name="removeAbandoned" value="true" />
  56. <!-- 1800秒,也就是30分钟 -->
  57. <property name="removeAbandonedTimeout" value="1800" />
  58. <!-- 关闭abanded连接时输出错误日志 -->
  59. <property name="logAbandoned" value="true" />
  60. <!-- 监控数据库 -->
  61. <!-- <property name="filters" value="stat" /> -->
  62. <property name="filters" value="mergeStat" />
  63. </bean>
  64.  
  65. <!-- ========================================MyBatis的配置项============================== -->
  66. <!-- 配置sqlSessionFactory -->
  67. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  68. <!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 -->
  69. <property name="dataSource" ref="dataSource" />
  70. <!-- <property name="configLocation" value="classpath:mybatis.cofig.xml" /> -->
  71. <!-- 自动扫描Mybatis的Mapper.xml文件 -->
  72. <property name="mapperLocations" value="classpath:zhu/mapper/*.xml" />
  73. </bean>
  74.  
  75. <!-- 配置扫描器 -->
  76. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  77. <!-- 扫描zhu.dao这个包以及它的子包下的所有映射接口类 -->
  78. <property name="basePackage" value="zhu.dao" />
  79. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
  80. </bean>
  81.  
  82. <!-- 配置事务管理器 -->
  83. <bean id="transactionManager"
  84. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  85. <property name="dataSource" ref="dataSource" />
  86. </bean>
  87.  
  88. <!-- 拦截器方式配置事物 -->
  89. <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
  90. <tx:attributes>
  91. <tx:method name="add*" propagation="REQUIRED" />
  92. <tx:method name="append*" propagation="REQUIRED" />
  93. <tx:method name="insert*" propagation="REQUIRED" />
  94. <tx:method name="save*" propagation="REQUIRED" />
  95. <tx:method name="update*" propagation="REQUIRED" />
  96. <tx:method name="modify*" propagation="REQUIRED" />
  97. <tx:method name="edit*" propagation="REQUIRED" />
  98. <tx:method name="delete*" propagation="REQUIRED" />
  99. <tx:method name="remove*" propagation="REQUIRED" />
  100. <tx:method name="repair" propagation="REQUIRED" />
  101. <tx:method name="delAndRepair" propagation="REQUIRED" />
  102. <tx:method name="get*" propagation="SUPPORTS" />
  103. <tx:method name="find*" propagation="SUPPORTS" />
  104. <tx:method name="load*" propagation="SUPPORTS" />
  105. <tx:method name="search*" propagation="SUPPORTS" />
  106. <tx:method name="datagrid*" propagation="SUPPORTS" />
  107. <tx:method name="*" propagation="SUPPORTS" />
  108. </tx:attributes>
  109. </tx:advice>
  110.  
  111. <aop:config>
  112. <!-- 配置所有的Advice,使用正则表达式,配置所有类,所有返回值的方法 -->
  113. <aop:pointcut id="transactionPointcut"
  114. expression="execution(* zhu.service..*Impl.*(..))" />
  115. <aop:advisor pointcut-ref="transactionPointcut"
  116. advice-ref="transactionAdvice" />
  117. </aop:config>
  118.  
  119. <!-- 配置druid监控spring jdbc -->
  120. <bean id="druid-stat-interceptor"
  121. class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean>
  122. <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
  123. scope="prototype">
  124. <property name="patterns">
  125. <list>
  126. <value>zhu.service.*</value>
  127. </list>
  128. </property>
  129. </bean>
  130.  
  131. <aop:config>
  132. <aop:advisor advice-ref="druid-stat-interceptor"
  133. pointcut-ref="druid-stat-pointcut" />
  134. </aop:config>
  135.  
  136. </beans>

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  6. <display-name></display-name>
  7.  
  8. <context-param>
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>classpath:applicationContext.xml, classpath:spring-mybatis.xml</param-value>
  11. </context-param>
  12.  
  13. <listener>
  14. <description>spring监听器</description>
  15. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  16. </listener>
  17.  
  18. <servlet>
  19. <description>spring mvc servlet</description>
  20. <servlet-name>springMvc</servlet-name>
  21. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  22. <init-param>
  23. <description>spring mvc 配置文件</description>
  24. <param-name>contextConfigLocation</param-name>
  25. <param-value>classpath:spring-mvc.xml</param-value>
  26. </init-param>
  27. <init-param>
  28. <param-name>activeReverseAjaxEnabled</param-name>
  29. <param-value>true</param-value>
  30. </init-param>
  31. <load-on-startup>1</load-on-startup>
  32. </servlet>
  33.  
  34. <servlet-mapping>
  35. <servlet-name>springMvc</servlet-name>
  36. <url-pattern>*.do</url-pattern>
  37. </servlet-mapping>
  38.  
  39. <filter>
  40. <filter-name>characterEncodingFilter</filter-name>
  41. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  42. <init-param>
  43. <param-name>encoding</param-name>
  44. <param-value>UTF-8</param-value>
  45. </init-param>
  46. <init-param>
  47. <param-name>forceEncoding</param-name>
  48. <param-value>true</param-value>
  49. </init-param>
  50. </filter>
  51.  
  52. <filter-mapping>
  53. <filter-name>characterEncodingFilter</filter-name>
  54. <url-pattern>/*</url-pattern>
  55. </filter-mapping>
  56.  
  57. <welcome-file-list>
  58. <welcome-file>login.jsp</welcome-file>
  59. </welcome-file-list>
  60.  
  61. </web-app>

web.xml

4):po包

EmpDeptVo.java

  1. package zhu.po;
  2.  
  3. import java.util.Date;
  4.  
  5. public class EmpDeptVo {
  6. private Integer eid; //员工id
  7. private String ename; //员工编号
  8. private int did; //部门id
  9. private boolean gende;//性别
  10. private int age; //年龄
  11.  
  12. private String workDate;//入职时间
  13. private String password;//密码
  14.  
  15. private String dname;//部门名称
  16.  
  17. public EmpDeptVo(){}
  18.  
  19. //用来查询数据
  20. public EmpDeptVo(Integer eid,String ename,boolean gende ,int age,String workDate,String password,int did,String dname ){
  21. this.eid=eid;
  22. this.ename=ename;
  23. this.gende=gende;
  24. this.age=age;
  25. this.workDate=workDate;
  26. this.password=password;
  27. this.did=did;
  28. this.dname=dname;
  29. }
  30.  
  31. public Integer getEid() {
  32. return eid;
  33. }
  34.  
  35. public void setEid(Integer eid) {
  36. this.eid = eid;
  37. }
  38.  
  39. public String getEname() {
  40. return ename;
  41. }
  42.  
  43. public void setEname(String ename) {
  44. this.ename = ename;
  45. }
  46.  
  47. public int getDid() {
  48. return did;
  49. }
  50.  
  51. public void setDid(int did) {
  52. this.did = did;
  53. }
  54.  
  55. public boolean isGende() {
  56. return gende;
  57. }
  58.  
  59. public void setGende(boolean gende) {
  60. this.gende = gende;
  61. }
  62.  
  63. public int getAge() {
  64. return age;
  65. }
  66.  
  67. public void setAge(int age) {
  68. this.age = age;
  69. }
  70.  
  71. public String getWorkDate() {
  72. return workDate;
  73. }
  74.  
  75. public void setWorkDate(String workDate) {
  76. this.workDate = workDate;
  77. }
  78.  
  79. public String getPassword() {
  80. return password;
  81. }
  82.  
  83. public void setPassword(String password) {
  84. this.password = password;
  85. }
  86.  
  87. public String getDname() {
  88. return dname;
  89. }
  90.  
  91. public void setDname(String dname) {
  92. this.dname = dname;
  93. }
  94.  
  95. }

EmpDeptVo.java

Tbdept.java

  1. package zhu.po;
  2.  
  3. public class Tbdept {
  4. private Integer did;
  5.  
  6. private String dname;
  7.  
  8. public Integer getDid() {
  9. return did;
  10. }
  11.  
  12. public void setDid(Integer did) {
  13. this.did = did;
  14. }
  15.  
  16. public String getDname() {
  17. return dname;
  18. }
  19.  
  20. public void setDname(String dname) {
  21. this.dname = dname;
  22. }
  23. }

Tbdept.java

Tbemp.java

  1. package zhu.po;
  2.  
  3. import java.util.Date;
  4.  
  5. public class Tbemp {
  6. private Integer eid;
  7.  
  8. private Integer did;
  9.  
  10. private Integer age;
  11.  
  12. private Boolean gende;
  13.  
  14. private Date workdate;
  15.  
  16. private String password;
  17.  
  18. private String ename;
  19.  
  20. public Integer getEid() {
  21. return eid;
  22. }
  23.  
  24. public void setEid(Integer eid) {
  25. this.eid = eid;
  26. }
  27.  
  28. public Integer getDid() {
  29. return did;
  30. }
  31.  
  32. public void setDid(Integer did) {
  33. this.did = did;
  34. }
  35.  
  36. public Integer getAge() {
  37. return age;
  38. }
  39.  
  40. public void setAge(Integer age) {
  41. this.age = age;
  42. }
  43.  
  44. public Boolean getGende() {
  45. return gende;
  46. }
  47.  
  48. public void setGende(Boolean gende) {
  49. this.gende = gende;
  50. }
  51.  
  52. public Date getWorkdate() {
  53. return workdate;
  54. }
  55.  
  56. public void setWorkdate(Date workdate) {
  57. this.workdate = workdate;
  58. }
  59.  
  60. public String getPassword() {
  61. return password;
  62. }
  63.  
  64. public void setPassword(String password) {
  65. this.password = password;
  66. }
  67.  
  68. public String getEname() {
  69. return ename;
  70. }
  71.  
  72. public void setEname(String ename) {
  73. this.ename = ename;
  74. }
  75. }

Tbemp.java

5):映射文件

TbdeptMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3.  
  4. <!--namespace命令空间,作用就是对sql进行分类化管理,理解sql隔离
  5. 注意:使用mapper代理方法开发,namepace有特殊重要的作用
  6. -->
  7. <mapper namespace="zhu.dao.TbdeptMapper" >
  8. <resultMap id="BaseResultMap" type="zhu.po.Tbdept" >
  9. <id column="did" property="did" jdbcType="INTEGER" />
  10. <result column="dname" property="dname" jdbcType="VARCHAR" />
  11. </resultMap>
  12. <sql id="Base_Column_List" >
  13. did, dname
  14. </sql>
  15.  
  16. </mapper>

TbempMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="zhu.dao.TbempMapper" >
  4. <resultMap id="BaseResultMap" type="zhu.po.Tbemp" >
  5. <id column="eid" property="eid" jdbcType="INTEGER" />
  6. <result column="did" property="did" jdbcType="INTEGER" />
  7. <result column="age" property="age" jdbcType="INTEGER" />
  8. <result column="gende" property="gende" jdbcType="BIT" />
  9. <result column="workDate" property="workdate" jdbcType="DATE" />
  10. <result column="password" property="password" jdbcType="VARCHAR" />
  11. <result column="ename" property="ename" jdbcType="VARCHAR" />
  12. </resultMap>
  13. <sql id="Base_Column_List" >
  14. eid, did, age, gende, workDate, password, ename
  15. </sql>
  16. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  17. select
  18. <include refid="Base_Column_List" />
  19. from tbemp
  20. where eid = #{eid,jdbcType=INTEGER}
  21. </select>
  22. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
  23. delete from tbemp
  24. where eid = #{eid,jdbcType=INTEGER}
  25. </delete>
  26. <insert id="insert" parameterType="zhu.po.Tbemp" >
  27. insert into tbemp (eid, did, age,
  28. gende, workDate, password,
  29. ename)
  30. values (#{eid,jdbcType=INTEGER}, #{did,jdbcType=INTEGER}, #{age,jdbcType=INTEGER},
  31. #{gende,jdbcType=BIT}, #{workdate,jdbcType=DATE}, #{password,jdbcType=VARCHAR},
  32. #{ename,jdbcType=VARCHAR})
  33. </insert>
  34. <insert id="insertSelective" parameterType="zhu.po.Tbemp" >
  35. insert into tbemp
  36. <trim prefix="(" suffix=")" suffixOverrides="," >
  37. <if test="eid != null" >
  38. eid,
  39. </if>
  40. <if test="did != null" >
  41. did,
  42. </if>
  43. <if test="age != null" >
  44. age,
  45. </if>
  46. <if test="gende != null" >
  47. gende,
  48. </if>
  49. <if test="workdate != null" >
  50. workDate,
  51. </if>
  52. <if test="password != null" >
  53. password,
  54. </if>
  55. <if test="ename != null" >
  56. ename,
  57. </if>
  58. </trim>
  59. <trim prefix="values (" suffix=")" suffixOverrides="," >
  60. <if test="eid != null" >
  61. #{eid,jdbcType=INTEGER},
  62. </if>
  63. <if test="did != null" >
  64. #{did,jdbcType=INTEGER},
  65. </if>
  66. <if test="age != null" >
  67. #{age,jdbcType=INTEGER},
  68. </if>
  69. <if test="gende != null" >
  70. #{gende,jdbcType=BIT},
  71. </if>
  72. <if test="workdate != null" >
  73. #{workdate,jdbcType=DATE},
  74. </if>
  75. <if test="password != null" >
  76. #{password,jdbcType=VARCHAR},
  77. </if>
  78. <if test="ename != null" >
  79. #{ename,jdbcType=VARCHAR},
  80. </if>
  81. </trim>
  82. </insert>
  83. <update id="updateByPrimaryKeySelective" parameterType="zhu.po.Tbemp" >
  84. update tbemp
  85. <set >
  86. <if test="did != null" >
  87. did = #{did,jdbcType=INTEGER},
  88. </if>
  89. <if test="age != null" >
  90. age = #{age,jdbcType=INTEGER},
  91. </if>
  92. <if test="gende != null" >
  93. gende = #{gende,jdbcType=BIT},
  94. </if>
  95. <if test="workdate != null" >
  96. workDate = #{workdate,jdbcType=DATE},
  97. </if>
  98. <if test="password != null" >
  99. password = #{password,jdbcType=VARCHAR},
  100. </if>
  101. <if test="ename != null" >
  102. ename = #{ename,jdbcType=VARCHAR},
  103. </if>
  104. </set>
  105. where eid = #{eid,jdbcType=INTEGER}
  106. </update>
  107. <update id="updateByPrimaryKey" parameterType="zhu.po.Tbemp" >
  108. update tbemp
  109. set did = #{did,jdbcType=INTEGER},
  110. age = #{age,jdbcType=INTEGER},
  111. gende = #{gende,jdbcType=BIT},
  112. workDate = #{workdate,jdbcType=DATE},
  113. password = #{password,jdbcType=VARCHAR},
  114. ename = #{ename,jdbcType=VARCHAR}
  115. where eid = #{eid,jdbcType=INTEGER}
  116. </update>
  117.  
  118. <!-- 增加查询所有的数据 的方法-->
  119. <select id="listAll" resultType="zhu.po.EmpDeptVo">
  120. SELECT tbdept.dname,tbemp.eid,tbemp.age,
  121. tbemp.gende,tbemp.workDate,tbemp.`password`,
  122. tbemp.ename FROM tbemp INNER JOIN tbdept ON tbdept.did = tbemp.did
  123. </select>
  124.  
  125. <!--在映射文件中配置很多sql语句 -->
  126. <!-- 需求:通过id查询员工表的记录 -->
  127. <!--通过select执行数据库的查询
  128. id:标签映射文件的sql,将sql语句封装 到mappedStatement对象中,
  129. 所以将id成为statement的id
  130. #{}表示一个占位符
  131. parameterType:指定输入参数的类型
  132. resultType:指定sql输出结果的所映射的java的对象类型,select指定resultType表示
  133. 将单挑记录映射成的Java对象
  134. -->
  135. <!-- 增加通过账号查询一条数据-->
  136. <select id="getByName" resultType="zhu.po.Tbemp">
  137. SELECT * FROM tbemp
  138. where tbemp.ename= #{ename,jdbcType=VARCHAR}
  139. </select>
  140. </mapper>

6):dao包

TbempMapper.java

  1. package zhu.dao;
  2.  
  3. import java.util.List;
  4.  
  5. import org.apache.ibatis.annotations.Param;
  6. import org.springframework.stereotype.Repository;
  7.  
  8. import zhu.po.EmpDeptVo;
  9. import zhu.po.Tbemp;
  10. @Repository(value = "empMapper")
  11. public interface TbempMapper {
  12.  
  13. int deleteByPrimaryKey(Integer eid);
  14. int insert(Tbemp record);
  15. int insertSelective(Tbemp record);
  16. Tbemp selectByPrimaryKey(Integer eid);
  17. int updateByPrimaryKeySelective(Tbemp record);
  18. int updateByPrimaryKey(Tbemp record);
  19. List<EmpDeptVo> listAll();
  20.  
  21. Tbemp getByName(@Param("ename") String ename);
  22. }

7):service包

IEmpService.java

  1. package zhu.service;
  2.  
  3. import java.util.List;
  4.  
  5. import zhu.po.EmpDeptVo;
  6. import zhu.po.Tbemp;
  7.  
  8. public interface IEmpService {
  9.  
  10. void save(Tbemp t);
  11.  
  12. void update(Tbemp t);
  13.  
  14. void delete(int id);
  15.  
  16. Tbemp getByID(int id);
  17.  
  18. List<EmpDeptVo> listAll();
  19.  
  20. Tbemp getByName(String name);
  21. }

IEmpService.java

EmpServiceImpl.java

  1. package zhu.service.impl;
  2.  
  3. import java.util.List;
  4.  
  5. import javax.annotation.Resource;
  6.  
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9.  
  10. import zhu.dao.TbempMapper;
  11. import zhu.po.EmpDeptVo;
  12. import zhu.po.Tbemp;
  13. import zhu.service.IEmpService;
  14.  
  15. @Service(value="myIEmpService")
  16. public class EmpServiceImpl implements IEmpService{
  17.  
  18. @Autowired
  19. private TbempMapper empMapper;
  20.  
  21. @Override
  22. public void save(Tbemp t) {
  23. // TODO Auto-generated method stub
  24. empMapper.insert(t);
  25. }
  26.  
  27. @Override
  28. public void update(Tbemp t) {
  29. // TODO Auto-generated method stub
  30. empMapper.updateByPrimaryKey(t);
  31. }
  32.  
  33. @Override
  34. public void delete(int id) {
  35. // TODO Auto-generated method stub
  36. empMapper.deleteByPrimaryKey(id);
  37. }
  38.  
  39. @Override
  40. public Tbemp getByID(int id) {
  41. // TODO Auto-generated method stub
  42. return empMapper.selectByPrimaryKey(id);
  43. }
  44.  
  45. @Override
  46. public List<EmpDeptVo> listAll() {
  47. // TODO Auto-generated method stub
  48. return empMapper.listAll();
  49. }
  50.  
  51. @Override
  52. public Tbemp getByName(String name) {
  53. // TODO Auto-generated method stub
  54. return empMapper.getByName(name);
  55. }
  56. }

EmpServiceImpl.java

8):web包

EmpAction.java

  1. package zhu.web;
  2.  
  3. import java.sql.Date;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.List;
  7.  
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Controller;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.servlet.ModelAndView;
  12.  
  13. import zhu.po.EmpDeptVo;
  14. import zhu.po.Tbemp;
  15. import zhu.service.IEmpService;
  16.  
  17. @Controller
  18. @RequestMapping("/EmpAction")
  19. public class EmpAction {
  20.  
  21. @Autowired
  22. private IEmpService myIEmpService;
  23.  
  24. // 登录账号
  25. @RequestMapping("/getByName")
  26. public ModelAndView getByName(String ename, String password) {
  27. try {
  28. Tbemp tbemp = myIEmpService.getByName(ename);
  29. if (tbemp.getEid() != null) {
  30. return listAll();
  31. }else{
  32. ModelAndView mv = new ModelAndView("/register");
  33. mv.addObject("ename", ename);
  34. return mv ;
  35. }
  36. } catch (Exception e) {
  37. ModelAndView mv = new ModelAndView("/register");
  38. mv.addObject("ename", ename);
  39. return mv ;
  40. }
  41.  
  42. }
  43.  
  44. // 查询所有的数据
  45. private ModelAndView listAll() {
  46. List<EmpDeptVo> emps = myIEmpService.listAll();
  47. ModelAndView mv = new ModelAndView("/jsp_CRUD/login_ok1");
  48. mv.addObject("empVo", emps);
  49. return mv;
  50. }
  51.  
  52. // 新增数据
  53. @RequestMapping("/save")
  54. public ModelAndView save(EmpDeptVo emp) {
  55. try {
  56. Tbemp tbEmp1 = new Tbemp();
  57. tbEmp1.setAge(emp.getAge());
  58. tbEmp1.setDid(emp.getDid());
  59. tbEmp1.setEname(emp.getEname());
  60. tbEmp1.setGende(emp.isGende());
  61. // request.getParameter("workDate");
  62. tbEmp1.setPassword(emp.getPassword());
  63. //将字符串转换为==》Data
  64. String workdate = emp.getWorkDate();
  65. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  66. Date sqlData = new Date(sdf.parse(workdate).getTime());
  67. tbEmp1.setWorkdate(sqlData);
  68. myIEmpService.save(tbEmp1);
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. return listAll();
  73. }
  74.  
  75. // 查询一条数据
  76. @RequestMapping("/findById")
  77. public ModelAndView findById(int eid) {
  78. Tbemp tbEmp1 = myIEmpService.getByID(eid);
  79. ModelAndView mView = new ModelAndView("/jsp_CRUD/update");
  80. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  81. String dateString = formatter.format(tbEmp1.getWorkdate());
  82. mView.addObject("e", tbEmp1);
  83. ////将Data转换为==》字符串
  84. mView.addObject("edata",dateString);
  85. return mView;
  86. }
  87.  
  88. // 修改数据
  89. @RequestMapping("/update")
  90. public ModelAndView update(EmpDeptVo emp) {
  91. try {
  92. Tbemp tbEmp1 = new Tbemp();
  93. tbEmp1.setAge(emp.getAge());
  94. tbEmp1.setDid(emp.getDid());
  95. tbEmp1.setEname(emp.getEname());
  96. tbEmp1.setGende(emp.isGende());
  97. tbEmp1.setEid(emp.getEid());
  98. tbEmp1.setPassword(emp.getPassword());
  99. //将字符串转换为==》Data
  100. String workdate = emp.getWorkDate();
  101. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  102. Date sqlData = new Date(sdf.parse(workdate).getTime());
  103. tbEmp1.setWorkdate(sqlData);
  104. myIEmpService.update(tbEmp1);
  105. } catch (ParseException e) {
  106.  
  107. e.printStackTrace();
  108.  
  109. }
  110. return listAll();
  111. }
  112.  
  113. // 删除
  114. @RequestMapping("/delete")
  115. public ModelAndView delete(int eid) {
  116. myIEmpService.delete(eid);
  117. return listAll();
  118.  
  119. }
  120. }

9):jsp

login_ok1.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11.  
  12. <title>所有数据</title>
  13.  
  14. <meta http-equiv="pragma" content="no-cache">
  15. <meta http-equiv="cache-control" content="no-cache">
  16. <meta http-equiv="expires" content="0">
  17. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  18. <meta http-equiv="description" content="This is my page">
  19.  
  20. </head>
  21.  
  22. <body>
  23. <div align="center" >
  24. <table cellspacing="0" border="1">
  25. <thead>
  26. <tr>
  27. <td>id</td>
  28. <td>编号</td>
  29. <td>年龄</td>
  30. <td>性别</td>
  31. <td>部门</td>
  32. <td>时间</td>
  33. <td>密码</td>
  34. <td>修改</td>
  35. <td>删除</td>
  36. </tr>
  37. </thead>
  38. <tbody>
  39. <c:forEach items="${empVo}" var="list2">
  40. <tr>
  41. <td>${ list2.eid}</td>
  42. <td>${ list2.ename}</td>
  43. <td>${ list2.age}</td>
  44. <c:if test="${ list2.gende==false}"><td></td></c:if>
  45. <c:if test="${ list2.gende==true}"><td></td></c:if>
  46. <td>${ list2.dname}</td>
  47. <td>${ list2.workDate}</td>
  48. <td>${ list2.password}</td>
  49. <td><a href="EmpAction/findById.do?eid=${list2.eid }" >修改</a></td>
  50. <td><a href="EmpAction/delete.do?eid=${list2.eid }" >删除</a></td>
  51. </tr>
  52. </c:forEach>
  53. </tbody>
  54. </table>
  55. </div>
  56. <hr/>
  57. <div align="center" >
  58. <form action="EmpAction/save.do" method="post">
  59. 编号:<input type="text" name="ename"/> <br/>
  60. 密码:<input type="text" name="password"/> <br/>
  61. 时间:<input type="text" name="workDate"/> <br/>
  62. 年龄:<input type="text" name="age"/> <br/>
  63. 性别:<select name="gende">
  64. <option value="0"></option>
  65. <option value="1"></option>
  66. </select> <br/>
  67. 部门:<select name="did">
  68. <option value="1">A部门</option>
  69. <option value="2">B部门</option>
  70. </select> <br/>
  71. <input type="submit" value="新增"/>
  72. </form>
  73. </div>
  74.  
  75. </body>
  76. </html>

login_ok1.jsp

update.jsp 

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11.  
  12. <title>My JSP 'update.jsp' starting page</title>
  13.  
  14. <meta http-equiv="pragma" content="no-cache">
  15. <meta http-equiv="cache-control" content="no-cache">
  16. <meta http-equiv="expires" content="0">
  17. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  18. <meta http-equiv="description" content="This is my page">
  19. <!--
  20. <link rel="stylesheet" type="text/css" href="styles.css">
  21. -->
  22.  
  23. </head>
  24.  
  25. <body>
  26. <hr/>
  27. <div align="center" >
  28. <form action="EmpAction/update.do" method="post">
  29. <input type="hidden" name="eid" value="${ e.eid}"/>
  30. 编号:<input type="text" name="ename" value="${ e.ename}"/> <br/>
  31. 密码:<input type="text" name="password" value="${ e.password}"/> <br/>
  32. 时间:<input type="text" name="workDate" value="${ edata}"/> <br/>
  33. 年龄:<input type="text" name="age" value="${ e.age}"/> <br/>
  34. 性别:<c:if test="${e.gende==false }">
  35. <select name="gende">
  36. <option value="0"></option>
  37. <option value="1"></option>
  38. </select>
  39. </c:if>
  40. <c:if test="${e.gende==true }">
  41. <select name="gende">
  42. <option value="1"></option>
  43. <option value="0"></option>
  44. </select>
  45. </c:if> <br/>
  46. 部门:<c:if test="${e.did==1 }">
  47. <select name="did" >
  48. <option value="1">A部门</option>
  49. <option value="2">B部门</option>
  50. </select>
  51. </c:if>
  52. <c:if test="${e.did==2 }">
  53. <select name="did" >
  54. <option value="2">B部门</option>
  55. <option value="1">A部门</option>
  56. </select>
  57. </c:if> <br/>
  58. <input type="submit" value="修改"/>
  59. </form>
  60. </div>
  61. <!--<input type="text" name="gende" value="${e.gende==true?'女':'男' }"><br> -->
  62. </body>
  63. </html>

update.jsp

运行效果:

下载源码链接:http://pan.baidu.com/s/1i5vOqxf  密码:lrgo

ssm框架的多表查询和增删查改的更多相关文章

  1. SSH框架的多表查询和增删查改 (方法一)上

    原创作品,允许转载,转载时请务必标明作者信息和声明本文章==>  http://www.cnblogs.com/zhu520/p/7772823.html   因为最近在做Android 练习的 ...

  2. SSH框架的多表查询和增删查改 (方法一)中

    原创作品,允许转载,转载时请务必标明作者信息和声明本文章==>http://www.cnblogs.com/zhu520/p/7774144.html   这边文章是接的刚刚前一遍的基础上敲的  ...

  3. SpringMVC框架的多表查询和增删查改

    必须声明本文章==>http://www.cnblogs.com/zhu520/p/7883268.html 一: 1):我的运行环境 我使用myeclipse(你也可以使用eclipse),t ...

  4. SQL Server 表的管理_关于表的操作增删查改的操作的详解(案例代码)

    SQL Server 表的管理_关于表的操作增删查改的操作的详解(案例代码) 概述: 表由行和列组成,每个表都必须有个表名. SQL CREATE TABLE 语法 CREATE TABLE tabl ...

  5. Mysql 单表操作、增删查改(基础4)

    新建一个表,往里面插入数据. #新建一个表 mysql> create table test( -> id int, -> name varchar(20) -> );Quer ...

  6. MyBatis3-实现单表数据的增删查改

    继续前一篇文章http://www.cnblogs.com/EasonJim/p/7050710.html所示的例子,返回的是单个实体,而接下来将进行列表的返回等操作: 一.查询列表 查询出列表,也就 ...

  7. mysql中数据表记录的增删查改(2)

    select `数据表.字段1`, group_concat(`数据表.字段2`) from `数据表` group by `数据表.字段1` order by `数据表.字段1` desc; sel ...

  8. mysql中数据表记录的增删查改(1)

    数据记录的增删改查 insert into `数据表名称` (`字段名称`, ...) values ('1', ...); delete from `数据表名称` where 子句; update ...

  9. SSH框架的多表查询(方法二)增删查改

     必须声明本文章==>http://www.cnblogs.com/zhu520/p/7773133.html  一:在前一个方法(http://www.cnblogs.com/zhu520/p ...

随机推荐

  1. GNU-libiconv编码转换库的使用举例

    继GDAL库.PROJ库.HDF5库.TINYXML库之后,手上进行的项目又让我碰到了ICONV库.之前花了2天时间没有搞定,在甲方一直催促下,今天又捡起来搞搞,搞了一天最终搞定了.相关心得记录例如以 ...

  2. delphi网络函数大全

    {=========================================================================功 能: 网络函数库时 间: 2002/10/02版 ...

  3. php使用flock堵塞写入文件和非堵塞写入文件

    php使用flock堵塞写入文件和非堵塞写入文件 堵塞写入代码:(全部程序会等待上次程序运行结束才会运行,30秒会超时) <?php $file = fopen("test.txt&q ...

  4. hdu_2871

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...

  5. 在 Ubuntu 18.04 LTS 无头服务器上安装 Oracle VirtualBox

    作者: Sk 译者: LCTT qhwdw | 2018-10-12 01:59 本教程将指导你在 Ubuntu 18.04 LTS 无头服务器上,一步一步地安装 Oracle VirtualBox. ...

  6. WSGI和CGI

    https://www.zhihu.com/question/19998865 https://segmentfault.com/a/1190000003069785

  7. ArcGIS api for javascript——地理编码任务-地理编码地址

    描述 本例允许用户输入一个地址,然后显示匹配的地址的位置.这通常地被称为地理编码.在ArcGIS JavaScript API中,使用Locator类执行地理编码. 定位器构造函数需要ArcGIS S ...

  8. Objective-C中的同步线程的锁

    概述 在多线程编程中往往会遇到多个线程同时访问共享的资源,这种情况我们需要通过同步线程来避免.也就是给线程加锁. 因为Objective-C是C语言的超集.,严格的来说是真超集.所以C语言当中的pth ...

  9. ORA-12514: TNS: 监听程序当前无法识别连接描写叙述符中请求的服务

    不指定数据库能够正常连接: 指定数据库和使用PL/SQL Developer都出现错误: 在此说明一下我的环境:Oralce装的是64位的在使用PL/SQL Developer时曾出现过初始化错误,解 ...

  10. unix mkdir命令的使用方法

    [语法]:   mkdir [-m 模式] [-p]  文件夹名 [说明]:  本命令用于建立文件夹,文件夹的存取模式由掩码(umask)决定,要求对其父文件夹具有写权限,文件夹的UID和GID为实际 ...