SSM框架的学习

1.基本配置文件  

applicationContext.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" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	
	
	

   <context:annotation-config />
	<context:component-scan base-package="cn.dxjava.www.service" />
	
  <!-- 加载jdbc属性文件 -->
 	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:mysql.properties"></property>
    </bean>
 	
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
	
	 <property name="driverClassName">  
	  <value>${mdriver}</value>
	  </property>  
	  <property name="url">  
	      <value>${murl}</value>
	  </property>  
	  <property name="username">  
	      <value>${musername}</value>
	  </property>  
	  <property name="password">  
	      <value>${mpassword}</value>
	  </property>
	 
	  	<!--     <value>com.mysql.jdbc.Driver</value>   
	  </property>  
	  <property name="url">  
	      <value>jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8</value>  
	
	  </property>  
	  <property name="username">  
	      <value>root</value>  
	  </property>  
	  <property name="password">  
	      <value>dxdx</value>  
	  </property>  --> 	
	  
	</bean>
	
	
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="typeAliasesPackage" value="cn.dxjava.www.pojo" />
		<property name="dataSource" ref="dataSource"/>
		<property name="mapperLocations" value="classpath:cn/dxjava/www/mapper/*.xml"/>
	</bean>

	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.dxjava.www.mapper"/>
	</bean>
	


</beans>
springMVC.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" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/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
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">



    <context:annotation-config/>

    <context:component-scan base-package="cn.dxjava.www.controller">
          <context:include-filter type="annotation" 
          expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <mvc:annotation-driven />
    
    <mvc:annotation-driven>
      <mvc:message-converters register-defaults="false">
 
            <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
 
            <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
 
                <property name="supportedMediaTypes">
 
                    <list>
 
                        <!-- 这里顺序不能反,一定先写text/html,不然ie下出现下载提示 -->
 
                       <!--  <value>text/html;charset=UTF-8</value> -->
 
                        <value>application/json;charset=UTF-8</value>
 
                    </list>
 
                </property>
 
            </bean>
 
        </mvc:message-converters>
     </mvc:annotation-driven>
    
    <mvc:default-servlet-handler />


    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
user.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.dxjava.www.mapper.UserMapper">

    <select id="login" parameterType="User" resultType="User">
	        select  * from user where uid=#{uid} and upsw=#{upsw}
	</select>
</mapper>
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_4_0.xsd"
         version="4.0">
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

    <!-- spring的配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- spring mvc核心:分发servlet -->
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- spring mvc的配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

2.  注意点  在controller需要返回数据时   加@ResponseBody    

3.使用注解查询用法

public interface  StudentMapper {
	
/*------------------------学生操作-----------------------------*/	
	public Student login(Student s);

	@Select("select * from student where sid = #{sid}")
	public Student findSelfInfo(Student s);
	
	@Update("update student set spsw = #{spsw},stel = #{stel} where sid = #{sid}")
	public int updateSelfInfo(Student s);
	
/*------------------------课程操作 -----------------------------*/	
	@Select("select * from course where cname like '%'||#{cname}||'%'")
	public List<Course> findCourse(Course c);
	
/*------------------------已选课程操作-----------------------------*/		
	@Insert("insert into selected_course values(#{cid},#{sid},sysdate)")
	public int insertSelectCourse(SelectedCourseInfo sci);
	
	@Select("select * from selected_course where sid = #{sid}")
	public List<SelectedCourseInfo> findHaveSelectedCourse(Student s);
	
	@Delete("delete from selected_course where cid = #{cid} and sid = #{sid}")
	public int deleteSelectedCourse(SelectedCourseInfo sci);

     @Update("update user set upsw = #{upsw} where uid = #{uid}")
    public int updatePsw(@Param("uid") int uid, @Param("upsw")String upsw);
}

猜你喜欢

转载自blog.csdn.net/qq_34232027/article/details/82254397