maven学习笔记3使用maven创建ssm 测试项目

在pom中增加 spring mvc的依赖信息

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.taobaogroupId>
  <artifactId>maven1artifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>warpackaging>
   <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    
    <spring.version>5.1.4.RELEASEspring.version>
    
    <mybatis.version>3.4.5mybatis.version>
    <mybatis-spring.version>1.3.1mybatis-spring.version>
    <mysql-connection-version>5.1.9mysql-connection-version>
    <jackson-version>2.9.4jackson-version>
  properties>
  <dependencies>
  
	<dependency>
	    <groupId>org.springframeworkgroupId>
	    <artifactId>spring-webmvcartifactId>
	    <version>5.1.4.RELEASEversion>
	dependency>
	
	    
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-jdbcartifactId>
   <version>5.0.8.RELEASEversion>
dependency>
       
    
    <dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>${mysql-connection-version}version>
dependency>

   <dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatis-springartifactId>
    <version>${mybatis-spring.version}version>
dependency>
   <dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatisartifactId>
    <version>${mybatis.version}version>
dependency>


<dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-databindartifactId>
    <version>${jackson-version}version>
dependency>
<dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-coreartifactId>
    <version>${jackson-version}version>
dependency>
<dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-annotationsartifactId>
    <version>${jackson-version}version>
dependency>

<dependency>
    <groupId>commons-dbcpgroupId>
    <artifactId>commons-dbcpartifactId>
    <version>1.4version>
dependency>
  dependencies>
project>

完成spring配置文件的编写

  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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.0.xsd  
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    "  
    default-lazy-init="true">  
    
    <context:component-scan base-package="com.mvc" />
    
    <mvc:annotation-driven >mvc:annotation-driven>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/">property>
    <property name="suffix" value=".jsp">property>
    bean>
    
    	
    <context:property-placeholder location="classpath:jdbc.properties" />
    
 	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
 		<property name="driverClassName" value="${jdbc.driver}">property>
 		<property name="url" value="${jdbc.url}">property>
 		<property name="username" value="${jdbc.username}">property>
 		<property name="password" value="${jdbc.password}">property>
 	bean>
  
 	
  	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
 		<property name="dataSource" ref="dataSource">property>
 		<property name="mapperLocations" value="classpath:com/mvc/beans/*.xml">property>
	bean> 
 	
 	
 	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 		<property name="basePackage" value="com.mvc.dao">property>
 	bean> 
    beans>

在web.xml 加载spirng的核心控制器

<servlet>
  	<servlet-name>springmvcservlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
  	<init-param>
  		<param-name>contextConfigLocationparam-name>
  		<param-value>classpath:springmvc.xmlparam-value>
  	init-param>
  servlet>
  <servlet-mapping>
  	<servlet-name>springmvcservlet-name>
  	<url-pattern>/url-pattern>
  servlet-mapping>

编写测试代码 java

  • 控制层
   @ResponseBody
   @RequestMapping("student_list")
   public List<Student> student_list(Model model){
   	return pd.slist();
   }
  • 数据访问层
public interface PageDao {

  @Select("select * from student")
  public List<Student> slist();
}
  • 实体对象
public class Student {
  
  String id;
  String name;
  String age;
  String sex;

编写测试页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${name}
</body>
</html>

访问测试页面ok

maven学习笔记3使用maven创建ssm 测试项目_第1张图片

你可能感兴趣的:(maven)