zoukankan      html  css  js  c++  java
  • ssm整合

    ssm整合

    项目目录

    jar

    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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    	<context:component-scan
    		base-package="com.yjf.service"></context:component-scan>
    	<bean id="propertyConfigurer"
    		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="location" value="classpath:db.properties" />
    	</bean>
    	<bean id="dataSource"
    		class="com.mchange.v2.c3p0.ComboPooledDataSource">
    		<property name="driverClass" value="${jdbc.driver}" />
    		<property name="jdbcUrl" value="${jdbc.url}" />
    		<property name="user" value="root" />
    		<property name="password" value="root" />
    		<property name="maxPoolSize" value="3"/>
    	</bean>
    <!-- 	<bean id="dataSource"
    		class="com.mchange.v2.c3p0.ComboPooledDataSource">
    		<property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
    		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm?autoReconnect=true&amp;failOverReadOnly=false&amp;characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true&amp;allowPublicKeyRetrieval=true" />
    		<property name="user" value="root" />
    		<property name="password" value="root" />
    	</bean> -->
    
    	<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    	<bean id="sqlSessionFactory"
    		class="org.mybatis.spring.SqlSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<!-- 自动扫描mapping.xml文件 -->
    		<property name="mapperLocations"
    			value="classpath:com/yjf/mapping/*.xml"></property>
    	</bean>
    
    	<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    		<property name="basePackage" value="com.yjf.dao" />
    		<property name="sqlSessionFactoryBeanName"
    			value="sqlSessionFactory"></property>
    	</bean>
    
    </beans>
    
    

    SpringMvc-config.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:mvc="http://www.springframework.org/schema/mvc"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    	<context:component-scan base-package="com.yjf.controller"></context:component-scan>
    	<mvc:annotation-driven></mvc:annotation-driven>
    	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/views/"></property>
    		<property name="suffix" value=".jsp"></property>
    	</bean>
    </beans>
    
    

    db.properties

    jdbc.driver=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/ssm?autoReconnect=true&failOverReadOnly=false&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
    jdbc.username=root
    jdbc.password=root
    
    

    log4j.properties

    log4j.rootLogger = debug,stdout
    log4j.appender.stdout = org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern = %5p  [%t]  - %m%n
    

    StudentController.java

    package com.yjf.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.yjf.entity.Student;
    import com.yjf.service.StudentService;
    
    @Controller
    @RequestMapping("/student")
    public class StudentController {
    	@Autowired
    	private StudentService studentservice;
    	@RequestMapping("/all")
    	public ModelAndView index() {
    		List<Student> res = studentservice.queryAll();
    		ModelAndView mv = new ModelAndView("index");
    		mv.addObject("res",res);
    		return mv;
    	}
    }
    
    

    StudentMapper.java

    package com.yjf.dao;
    
    import java.util.List;
    
    import org.springframework.stereotype.Repository;
    
    import com.yjf.entity.Student;
    
    @Repository
    public interface StudentMapper {
    	List<Student> queryAll();
    }
    
    

    Student.java

    package com.yjf.entity;
    
    public class Student {
    	private int id;
    	private String name;
    	private int age;
    	private String nickname;
    	private String password;
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	public String getNickname() {
    		return nickname;
    	}
    	public void setNickname(String nickname) {
    		this.nickname = nickname;
    	}
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    	
    }
    
    

    StudentMapper.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="com.yjf.dao.StudentMapper">
    	<select id="queryAll" resultType="com.yjf.entity.Student" >
    		select * from student
    	</select>
    	
    	
    </mapper>
    

    index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    
    <table border="1">
    <c:forEach items="${requestScope.res }" var="stu">
    	<tr>
    		<td>${stu.id }</td>
    		<td>${stu.name }</td>
    		<td>${stu.age }</td>
    		<td>${stu.nickname }</td>
    		<td>${stu.password }</td>
    	</tr>
    	</c:forEach>
    </table>
    
    
    </body>
    </html>
    

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>SSMTest</display-name>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    	<servlet>
    		<servlet-name>springDispatcherServlet</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>classpath:SpringMvc-config.xml</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>springDispatcherServlet</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
      
    	<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>
    </web-app>
    

    mysql数据库

  • 相关阅读:
    Java Image Processing
    贝塞尔曲线开发的艺术
    Ubuntu中Hadoop环境搭建
    FIRST集合、FOLLOW集合、SELECT集合以及预测分析表地构造
    Linux环境下使用VSCode编译makefile文件的注意事项
    神经记忆模型
    深度学习推荐阅读的论文
    博客园无法发布文章解决办法
    计算机各个方向名校公开课
    软件过程基础
  • 原文地址:https://www.cnblogs.com/sm1128/p/11303687.html
Copyright © 2011-2022 走看看