SSM编码流程

来源:互联网 发布:内裤淘宝主图设计 编辑:程序博客网 时间:2024/05/16 15:53

导入jar

https://yunpan.cn/cqA8CXVUUfmHk 提取码 e72d

1spring-mvc.xml配置文件

<?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"  

   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    

                      http://www.springframework.org/schema/context    

                      http://www.springframework.org/schema/context/spring-context-3.1.xsd   

                      http://www.springframework.org/schema/mvc    

                      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  

                       

   <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->  

   <context:component-scan base-package="com.t5.client.action"/> 

   <context:component-scan base-package="com.t5.manage.action"/> 

   <!--  需要添加该配置 -->  

   <mvc:annotation-driven/>

   <mvc:default-servlet-handler/>

   <!-- 避免IE执行AJAX时,返回JSON出现下载文件   -->

<beanid="mappingJacksonHttpMessageConverter"  

      class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 

       <property name="supportedMediaTypes">  

           <list>  

              <value>text/html;charset=UTF-8</value>  

           </list>  

       </property>  

   </bean>  

   <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->  

   <bean  

      class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 

       <property name="messageConverters">  

           <list>  

               <refbean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->  

           </list>  

       </property>  

   </bean>  

   <!-- 定义跳转的文件的前后缀,视图模式配置-->  

   <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"> 

       <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个可用的url地址 -->  

       <property name="prefix" value="/" /> 

       <property name="suffix" value=".jsp" /> 

   </bean>   

 </beans>


​2
spring-mybatis.xml配置

<?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"

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    

                      http://www.springframework.org/schema/context    

                      http://www.springframework.org/schema/context/spring-context-3.1.xsd   

                      http://www.springframework.org/schema/mvc    

                      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<!-- 自动扫描 -->

<context:component-scanbase-package="com.t5.client" />

<context:component-scanbase-package="com.t5.manage" />

<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close">

<propertyname="driverClassName" value="com.mysql.jdbc.Driver" />

<propertyname="url" value="jdbc:mysql://localhost:3306/test" />

<propertyname="username" value="root" />

<propertyname="password" value="admin" />

<!-- 初始化连接大小 -->

<propertyname="initialSize" value="0"></property>

<!-- 连接池最大数量 -->

<propertyname="maxActive" value="20"></property>

<!-- 连接池最大空闲 -->

<propertyname="maxIdle" value="20"></property>

<!-- 连接池最小空闲 -->

<propertyname="minIdle" value="1"></property>

<!-- 获取连接最大等待时间 -->

<propertyname="maxWait" value="60000"></property>

</bean>

<!-- springMyBatis完美整合,不需要mybatis的配置映射文件 -->

<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">

<propertyname="dataSource" ref="dataSource" />

<!-- 自动扫描mapping.xml文件 -->

<propertyname="mapperLocations"value="classpath:com/t5/*/dao/mapping/*.xml"></property>

</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->

<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">

<propertyname="basePackage" value="com.t5.*.dao" />

<propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"></property>

</bean>

<!--

(事务管理)transactionmanager, use JtaTransactionManager for global tx

-->

<beanid="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<propertyname="dataSource" ref="dataSource" />

</bean>

<mvc:annotation-driven/>

<mvc:default-servlet-handler/>

</beans> 

​3web.xml配置

<?xmlversion="1.0" encoding="UTF-8"?>

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

<display-name>ArchetypeCreated Web Application</display-name>

<!-- Springmybatis的配置文件 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring-mybatis.xml</param-value>

</context-param>

<!-- 编码过滤器 -->

<filter>

<filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<async-supported>true</async-supported>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!-- Spring监听器 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!-- 防止Spring内存溢出监听器 -->

<listener>

<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

</listener>

<!-- SpringMVC servlet -->

<servlet>

<servlet-name>SpringMVC</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring-mvc.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

<async-supported>true</async-supported>

</servlet>

<servlet-mapping>

<servlet-name>SpringMVC</servlet-name>

<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->

<url-pattern>/</url-pattern>

</servlet-mapping>

<servlet> 

<servlet-name>DisplayChart</servlet-name> 

<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> 

</servlet> 

<servlet-mapping> 

<servlet-name>DisplayChart</servlet-name> 

<url-pattern>/DisplayChart</url-pattern> 

</servlet-mapping> 

<welcome-file-list>

<welcome-file>/index.jsp</welcome-file>

</welcome-file-list>

</web-app>



​4)编写实体类(studentscoregrade,写多个实体类的话能写出mybatis配置关系)

public classStudent {

private intstudentId;//主键标识列

private StringstudentName;//学生名字

private intstudentAge;//学生年龄

private GradegradeId;//年级外键

省略SetGet方法

}

public classScore {

private intscoreId;//主键

private doublescoreNum;//分数

private StringsubjectName; //科目

private Studentstudentid;//学生表外键

省略SetGet方法

}

​public classGrade {

private intgradeId;//主键

private StringgradeName;//年级名称

省略SetGet方法

}​

5.编写dao

public interfaceScoreDao {

//查询所有学生信息

publicList<Score> studentAll();

}

6.编写mybatis的配置文件ScoreDaoMapper.xml

<?xmlversion="1.0" encoding="UTF-8"?>

<!DOCTYPEmapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""mybatis-3-mapper.dtd" >

<mappernamespace="com.t5.client.dao.ScoreDao">

<resultMaptype="com.t5.entity.Score" id="entityScore">

<idcolumn="score_id" property="scoreId" />

<resultcolumn="score_num" property="scoreNum" />

<resultcolumn="subject_name" property="subjectName" />

<associationproperty="studentid" javaType="com.t5.entity.Student"resultMap="entityStudent">

<idcolumn="student_id" property="studentId" />

<resultcolumn="student_name" property="studentName" />

<resultcolumn="student_age" property="studentAge" />

<resultcolumn="grade_id" property="gradeId" />

</association>

</resultMap>

<resultMaptype="com.t5.entity.Student" id="entityStudent">

<idcolumn="student_id" property="studentId" />

<resultcolumn="student_name" property="studentName" />

<resultcolumn="student_age" property="studentAge" />

<associationproperty="gradeId" javaType="com.t5.entity.Grade">

<idcolumn="grade_id" property="gradeId" />

<resultcolumn="grade_name" property="gradeName" />

</association>

</resultMap>

<!--  iddao方法名一致,方法中需要用到什么表配置什么表 -->

<selectid="studentAll" resultMap="entityScore">

       SELECT

*

FROM

score s,

student d,

grade g

WHERE

s.student_id =d.student_id

AND d.grade_id =g.grade_id

 </select> 

</mapper>

7.编写biz​

public interfaceScoreService {

publicList<Score> scoreAllService();

}

8.编写biz实现类bizImpl

@Service("scoreService")

public classScoreServiceImpl implements ScoreService{

@Resource

private ScoreDaoscoredao;//使用dao接口

publicList<Score> scoreAllService() {

returnscoredao.studentAll();

}

}

9.编写Action​

@Controller

@RequestMapping(value= "ScoreAction")

public classScoreAction {

@Resource

privateScoreService scoreSer;

@RequestMapping(value= "ScoreAll")

public StringscoreAll(){

List<Score>list=scoreSer.scoreAllService();

//遍历

for (int i = 0;i < list.size(); i++) {

System.out.println("年级:"+list.get(i).getStudentid().getGradeId().getGradeName()+"\t分数:"+list.get(i).getScoreNum()+"\t学生名:"+list.get(i).getStudentid().getStudentName());

}

//配置文件中会自动在后面追加.jsp​

return"AllList";

}

}

测试

​<ahref="ScoreAction/ScoreAll">Test</a>

控制台输出图 --->



注:这是基础的ssm编码流程,重点是流程,和配置多表查询

 

0 0
原创粉丝点击