关于spring+springMVC+myBatis的一些基础配置以及整合

来源:互联网 发布:松下bw530c网络传输 编辑:程序博客网 时间:2024/05/18 18:44

不说了多了   直接上代码  首先是从程序的入口开始说:

 web.xml



<?xml version="1.0"encoding="UTF-8"?>
<web-appversion="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
   
  <!--
  设置session过期的时间
  -->
  <session-config>
  <session-timeout>20</session-timeout>
  </session-config>
   
  <!--
  读取spring的配置文件
  -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:config/spring.xml;classpath:config/spring-myBatis.xml</param-value>
  </context-param>
   
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
   
   <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
   
<!--
  设置字符编码,将所有的字符编码同意设置为utf-8
  -->
  <filter>
  <filter-name>filterEncoding</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>filterEncoding</filter-name>
  <url-pattern>/</url-pattern>
  </filter-mapping>
   
  <!--
  生成一次性验证码的servlet
  -->
  <servlet>
  <servlet-name>verifyCode</servlet-name>
  <servlet-class>com.longhang.tool.verifyCode.VerifyCodeServlet</servlet-class>
  </servlet>
   
  <!--
  将所有*.do的请求交给springMVC的DispatcherServlet来处理
  -->
  <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:config/springMVC-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
   
  <servlet-mapping>
  <servlet-name>verifyCode</servlet-name>
  <url-pattern>/verifyCode</url-pattern>
  </servlet-mapping>
   
  <servlet-mapping>
  <servlet-name>DispatcherServlet</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

springMVC的配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xmlversion="1.0"encoding="UTF-8"?>  
 
 
  <beansxmlns="http://www.springframework.org/schema/beans"    
 
 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"    
 
 
        xmlns:context="http://www.springframework.org/schema/context"    
 
 
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
 
 
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
 
 
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
 
 
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
 
 <!--
  配置自动扫描的包,让其扫描    com.longhang,controller下面的所有包
 -->
   
  <context:component-scanbase-package= "com.longhang.controller"></context:component-scan>
   
 <!-- 
  配置视图解析器
  将视图逻辑名解析为/*.jsp
 -->
  <beanclass= "org.springframework.web.servlet.view.InternalResourceViewResolver">
  <propertyname= "prefix"value= "/"></property>
  <propertyname= "suffix"value= ".jsp"></property>
  </bean>
  </beans>

spring.xml的配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"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-3.0.xsd
">
<context:property-placeholderlocation="classpath:config/druid.properties"/>
<!-- 自动扫描(自动注入) -->
<context:component-scanbase-package= "com.longhang.service"></context:component-scan>
 
 
</beans>


spring-myBatis.xml配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
    <!-- 配置数据源 --><!--
    <bean name = "datasource" class = "com.alibaba.druid.pool.DruidDataSource" init-method = "init" destroy-method = "close">
    <property name ="url" value = "${jdbc_url}"></property>
    <property name="username" value="${jdbc_userName}" />
 <property name="password" value="${jdbc_password}" />
    </bean>
     
     --><beanname= "datasource"class= "com.alibaba.druid.pool.DruidDataSource"init-method= "init"destroy-method= "close">
    <propertyname="url"value= "jdbc:mysql://localhost:8000/bookShopping"></property>
    <propertyname="username"value="root"/>
 <propertyname="password"value="13072399672"/>
     </bean>
     
    <!--配置sqlSessionFactory 并读取mybatis的一些配置-->
    <beanname= "sqlSessionFactory"class= "org.mybatis.spring.SqlSessionFactoryBean">
    <propertyname= "dataSource"ref= "datasource"></property>
    <propertyname="mapperLocations"value="classpath:mapper/*.xml"/>
    </bean>
     
    <!--
    自动扫描 将Mapper接口生成代理注入到Spring
    -->
    <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage"value="com.longhang.dao"/>
<propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>
</bean>
     
    <!--
    配置事物
    -->
   <beanid= "transactionManager"class= "org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <propertyname= "dataSource"ref= "datasource"></property>
    </bean>
     
    <!--
    <tx:annotation-driven transaction-manager = "transactionManager"/>
    -->
   
   <!--
    事物的具体内容
   -->
<tx:adviceid="transactionAdvice"transaction-manager="transactionManager">
<tx:attributes>
<tx:methodname="add*"propagation="REQUIRED"/>
<tx:methodname="append*"propagation="REQUIRED"/>
<tx:methodname="insert*"propagation="REQUIRED"/>
<tx:methodname="save*"propagation="REQUIRED"/>
<tx:methodname="update*"propagation="REQUIRED"/>
<tx:methodname="modify*"propagation="REQUIRED"/>
<tx:methodname="edit*"propagation="REQUIRED"/>
<tx:methodname="delete*"propagation="REQUIRED"/>
<tx:methodname="remove*"propagation="REQUIRED"/>
<tx:methodname="repair"propagation="REQUIRED"/>
<tx:methodname="delAndRepair"propagation="REQUIRED"/>
 
 
<tx:methodname="get*"propagation="SUPPORTS"/>
<tx:methodname="find*"propagation="SUPPORTS"/>
<tx:methodname="load*"propagation="SUPPORTS"/>
<tx:methodname="search*"propagation="SUPPORTS"/>
<tx:methodname="datagrid*"propagation="SUPPORTS"/>
 
 
<tx:methodname="*"propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
 
<!--
    定义一个切面,在定义的切面上加入事物
   -->
    <aop:config>
<aop:pointcutid="transactionPointcut"expression="execution(* com.longhang.service..*Impl.*(..))"/>
<aop:advisorpointcut-ref="transactionPointcut"advice-ref="transactionAdvice"/>
</aop:config>
 
</beans>

关于mybatis的映射文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mappernamespace="com.longhang.dao.userDao.UserDao">
  <resultMapid= "baseResultMap"type= "com.longhang.entity.user.User">
  <idcolumn= "uid"property= "uid"jdbcType= "CHAR"/>
  <resultcolumn= "loginname"property= "loginname"jdbcType= "VARCHAR"/>
  <resultcolumn= "loginpass"property= "loginpass"jdbcType= "VARCHAR"/>
  <resultcolumn= "email"property= "email"jdbcType= "VARCHAR"/>
  <resultcolumn= "status"property= "status"jdbcType= "VARCHAR"/>
  <resultcolumn= "activationCode"property= "activationCode"jdbcType= "CHAR"/>
  </resultMap>
   
   
  <sqlid= "base_column_list">
  uid,loginname,loginpass,email,status,activationCode
  </sql>
  
  <!--根据id查询
   返回的类型为User
  -->
  <selectid="findById"resultMap="baseResultMap"parameterType="java.lang.String">
    select
    <includerefid="base_column_list"/>
    from l_user
    where uid = #{uid,jdbcType=CHAR}
  </select>
   
  <!--
  根据activationCode查询
  返回值是User
  -->
   
  <selectid= "findByActivationCode"resultMap= "baseResultMap"parameterType= "java.lang.String">
  select <includerefid= "base_column_list"/>
  from l_user
  where activationCode = #{activationCode}
  </select>
   
  <!--
  根据loginname查看总条数
  -->
  <selectid= "countNumberByLoginName"resultType= "int"parameterType="java.lang.String">
  select count(*) from l_user where loginname = #{loginname}
  </select>
   
  <!--
  根据email查看总条数
   -->
   <selectid= "countNumberByEmail"resultType= "int"parameterType= "java.lang.String">
    select count(*) from l_user where email = #{email}
   </select>
    
   <!--
    
   -->
   <selectid= "isActivation"resultType= "int"parameterType= "java.lang.String">
   select count(*) from l_user where activationCode = #{activationCode}
   </select>
 
 
  <deleteid= "deleteById"parameterType= "java.lang.String">
  delete from l_user
  where uid = #{uid,jdbcType=CHAR}
  </delete>
   
   
<insertid= "addUser"parameterType= "com.longhang.entity.user.User">
  insert into l_user(uid,loginname,loginpass,email,status,activationCode)
  values(#{uid},#{loginname},#{loginpass},#{email},#{status},#{activationCode})
 </insert>
    <!--
  <insert id="insertSelective" parameterType="com.bky.model.Add" >
    insert into tadd
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="tname != null" >
        tname,
      </if>
      <if test="tpwd != null" >
        tpwd,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="tname != null" >
        #{tname,jdbcType=VARCHAR},
      </if>
      <if test="tpwd != null" >
        #{tpwd,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>-->
  <!--
  <update id="updateByPrimaryKeySelective" parameterType="com.bky.model.Add" >
    update tadd
    <set >
      <if test="tname != null" >
        tname = #{tname,jdbcType=VARCHAR},
      </if>
      <if test="tpwd != null" >
        tpwd = #{tpwd,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>-->
 
 
  <updateid= "updateById"parameterType="com.longhang.entity.user.User">
  update l_user
  set uid = #{uid},
     loginname = #{loginname},
     loginpass = #{loginpass},
     email = #{email},
     status = #{status},
     activationCode = #{activationCode}
     where uid = #{uid}
  </update>
   
  <updateid="updatePropByID">
update  l_user set
<foreachcollection="prop"item="item"index="index"separator=",">
          ${item.key} = #{item.value}
        </foreach>
        <where>
        uid = #{uid}
        </where>
</update>
 
<updateid="updatePropByCondition">
<![CDATA[ 
update  l_user set
]]>
<foreachcollection="prop"item="item"index="index"separator=",">
          ${item.key} = #{item.value}
        </foreach>
        <where>
        <iftest="condition != null">
        ${condition}
        </if>
        </where>
</update>
   
  <selectid="getAll"resultMap="baseResultMap">
  SELECT * FROM uid
  </select>
</mapper>

接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.longhang.dao.userDao;
 
 
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
 
 
import org.apache.ibatis.annotations.Param;
 
 
import com.longhang.entity.user.User;
 
 
public interface UserDao {
public User findById(String uid);
public int deleteById(String uid);
public int addUser(User user);
public int updateById(String uid);
public List<User> getAll();
  
public int countNumberByLoginName(String loginname);
public int countNumberByEmail(String email);
public int isActivation(String activationCode);
public User findByActivationCode(String activationCode);
  
public int updatePropByID(@Param("prop")Set<Entry<String, Object>> prop,@Param("uid")String id);
public int updatePropByCondition(@Param("prop")Set<Entry<String,Object>> prop,@Param("condition")String condition);
}
1 0