杂谈maven工程实践(3)

1、常见应用

maven工程中,主要操作的是pom.xml配置和java开发

java开发:代码&配置文件

pom.xml:管理maven功臣资源、依赖、插件等信息

 

 

2、资源管理

包括:jar包的资源管理、工程之家女的关系管理,常用信息的配置管理

 

资源定义

案例:开发mybatis的测试

定义:需要的jar包

资源来源:http://mvnrepository.com

 

 

资源的依赖

 

 

测试使用mybatis进行查询

 

 

mybatisconfig,xml




     
     
     
     
           
                
                
                     
                     
                     
                     
                
           
     
     
     
      
     
     

  

db.propertis

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/users
jdbc.username=root
jdbc.password=1234

  

UserMapper.xml

"1.0" encoding="UTF-8" ?>
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
namespace="test">
     
     <select id="select" parameterType="int"  resultType="com.pojo.User">
           select * from user where id = #{id}
     select>
   

 

 

User.java

package com.pojo;

public class User {

private int id;
private String name;

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 User() {
}
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
}

 

 

tests.java

public class tests {
     
     @Test
     public void testss() throws IOException{
           
           //配置文件的select
        String resource = "mybatisconfig.xml";
        //得到配置文件流
        InputStream inputStream =  Resources.getResourceAsStream(resource);
        
        //创建会话工程
        SqlSessionFactory sessionFactory = new  SqlSessionFactoryBuilder().build(inputStream);
  
        //通过工程得到Session
        SqlSession session = sessionFactory.openSession();
        
        //通过SqlSession操作数据库
        //session.selectOne(arg0, arg1)
        User user = session.selectOne("test.select", 2);
        System.out.println(user);
        
        session.close();
           
     }
}

 

 

 

 

3、本地安装

 

 

右键工程--》run as --》maven install

 

 

测试启动

 

 

服务启动

 

 

测试springmvc的使用进行启动tomcat
事务管理加载tx
   
     
         org.springframework
         spring-context
         4.3.10.RELEASE
     
           
           
     
         org.springframework
         spring-beans
         4.3.10.RELEASE
     
     
     
     
         org.springframework
         spring-aspects
         4.3.10.RELEASE
     

     
         org.springframework
         spring-webmvc
         4.3.10.RELEASE
     
     
         org.springframework
         spring-tx
         4.3.10.RELEASE
     
           
       
         org.springframework
         spring-jdbc
         4.3.10.RELEASE
     

 

 
            
           
               javax.servlet
               servlet-api
               2.5
               provided
           
            
           
               javax.servlet.jsp
               jsp-api
               2.2
               provided
           
                
           
               javax.servlet
               jstl
               1.2
           
使用tomcat
https://blog.csdn.net/sunroyfcb/article/details/80850779

     
     
           
         org.apache.tomcat.maven
            tomcat7-maven-plugin
            2.2     
        
        
     
     
     
     
           
              org.apache.tomcat.maven
                 tomcat7-maven-plugin
                    
                       /
                       8081
                       tomcat7
                    
                     
            
     

 

 

 

 

TestController.java

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

    @RequestMapping("/test")
    public String test(){
        System.out.println("test.....");
        return "success";
    }
}

 

applicationContext.xml

"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
base-package="com.controller">
           

class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     "prefix" value="/WEB-INF/jsp">
     "suffix" value=".jsp">

 

 
web.xml
"1.0" encoding="UTF-8"?>
"http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://xmlns.jcp.org/xml/ns/javaee"  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"  version="3.1">

     login.jsp

  
  
  
      springmvc
      class>org.springframework.web.servlet.DispatcherServletclass>
      
           contextConfigLocation
           classpath:cn/com/springmvc/applicationContext.xml
      
   
    1
  
  
      springmvc
      /
  

success.jsp

<%@ page language="java" import="java.util.*"  pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath =  request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
"-//W3C//DTD HTML 4.01 Transitional//EN">

  
    <base href="<%=basePath%>">
    
    My JSP <span style="color:#800000;">'</span><span style="color:#800000;">success.jsp</span><span style="color:#800000;">'</span> starting page
    
     "pragma" content="no-cache">
     "cache-control" content="no-cache">
     "expires" content="0">    
     "keywords"  content="keyword1,keyword2,keyword3">
     "description" content="This is my page">
     
  
  
  
   maven中的springmvc测试!!!
  

 

 

 

 

 

 

 

 

tomcat:deploy   --部署一个web war包
tomcat:reload   --重新加载web war包
tomcat:start    --启动tomcat
tomcat:stop    --停止tomcat
tomcat:undeploy--停止一个war包
tomcat:run  启动嵌入式tomcat ,并运行当前项目

 

 

 

 

 

 

转载于:https://www.cnblogs.com/Mrchengs/p/11502477.html

你可能感兴趣的:(杂谈maven工程实践(3))