idea搭建meven项目
先到maven官网下载压缩包,解压就行,然后配置环境变量:
maven的使用要配置环境变量:(配置之前要检查jdk的环境变量是否配置成功)
打开环境变量:1.创建maven_home,值是maven的路径,例如:D:\maven\apache-maven-3.6.3(路径里不要有中文)
2.在path里添加 ;%MAVEN_HOME%\bin;
配置之后win+r后输入cmd打开管理员面板输入mvn -v命令,如果出现版本信息就成功
maven下载地址:https://maven.apache.org/download.cgi
使用之前先配置maven下边的conf\settings.xml的本地仓库和远程仓库,仓库里存的是jar包,当项目开始时本地仓库没有需要的jar包,会从远程仓库下载保存到本地仓库:
本地仓库: <localRepository>D:\aaamaven\mavenhome</localRepository> 远程仓库,使用的阿里云: <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror>
在idea中添加maven环境:
File -->settings-->maven:
开始搭建maven项目:
1、File--->New--->Project,在新弹出来的窗口中左边栏选择Maven,右边栏maven-archetype-webapp
2、在新出窗口中需要填两个地方,Groupid和Artifactid,Groupid为组织名,例如:com.xxxx。Artifactid为项目名
3、下边页面点击Next,后边页面点击Finish,完成项目创建
4、maven文件创建之后并没有放置java代码的文件夹以及放置资源文件的文件夹(资源文件夹中放置包含mapper.xml文件),需要手动创建这两个 文件夹,文件创建完之后要将文件进行转化。存放java的文件标记步骤:右击文件夹,选择"Mark Directory as”---->"Test sources Root"。存放资源文件标记步骤:右击文件夹,选择"Mark Directory as”---->"Resources Root"。本人
新建项目目录如图:
注:target文件是项目编译之后产生的文件
项目代码:
pom.xml:pom文件中配置项目依赖的各种jar,项目启动之后就根据pom.xml去网上下载,本地有了就不用下载了
<?xml version="1.0" encoding="UTF-8"?> <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.0</modelVersion> <groupId>com.xxzg</groupId> <artifactId>movendemo1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>movendemo1 Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <spring.version>5.1.1.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--spring依赖的jar--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!--数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!--lombok:简化实体类的编写--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> <!--支持web--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!--引入mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.1</version> </dependency> <!--处理json--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.5</version> </dependency> <!--解决Spring使用slf4j输出日志与log4j冲突的问题--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.6</version> </dependency> <!-- log4j的包 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <!--mybatis自动生成代码的jar--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.6</version> </dependency> <!--文件上传--> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <!-- <dependency> <groupId>eu.bitwalker</groupId> <artifactId>UserAgentUtils</artifactId> <version>1.19</version> </dependency> --> <!--分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.9</version> </dependency> <!--引入shiro--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.3.2</version> </dependency> </dependencies> <build> <finalName>movendemo1</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> <plugins> <!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.6</version> <configuration> <!--指定配置文件的名称。默认值:${basedir}/src/main/resources/generatorConfig.xml--> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <!--新生成的文件会覆盖原有的文件。--> <overwrite>true</overwrite> <!--如果指定该参数,执行过程会输出到控制台--> <verbose>true</verbose> </configuration> </plugin> </plugins> </build> </project>
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"> <context-param> <!--spring的配置文件的地址--> <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> <!--配置编码过滤器,要配置在所有过滤器的前面--> <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> <!--前端控制器--> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--servlet的初始化参数,只能被DispatcherServlet识别--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dispatcher-servlet.xml</param-value> </init-param> <!-- load-on-statup 的作用 1.load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。 2.它的值必须是一个整数,表示servlet应该被载入的顺序。; 3.当值为0或者大于0时,表示容器在启动时就加载并初始化这个servlet。 4.当值小于0或者没有指定时,则表示容器在该Servlet被请求时,才会去加载。 5.正数的值越小,该Servlet的优先级就越高,应用启动时就优先加载。 6.当值相同的时候,容器就会自己选择优先加载。 所以,<load-on-startuo>x</load-on-startuo>中x的取值1,2,3,4,5代表的是优先级,而非启动延迟时间。--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
资源文件:
1、spring的配置文件
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--<context:component-scan>:配置自动扫描注解的包--> <context:component-scan base-package="com.xxzg"></context:component-scan> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!--配置数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <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> <!--创建sqlSessionFactory,这是配置mybatis--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--引入数据源--> <property name="dataSource" ref="dataSource"></property> <!--如果使用mybatis的配置文件--> <!--<property name="configLocation" value="classpath:mybatis-spring.xml"></property>--> <!--配置dao接口的映射文件--> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> <!--设置别名--> <property name="typeAliasesPackage" value="com/xxzg/entity"></property> </bean> <!--配置扫描器,扫描dao接口,当利用mybatis时,dao接口就没有了实现类,需要扫描器做一些处理--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com/xxzg/dao"></property> </bean> <!--配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--事务的注解配置--> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> </beans>
2、spring里配置数据源需要的配置文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/springtest jdbc.username=root jdbc.password=root
3、springmvc的配置文件
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--开启mvc注解--> <mvc:annotation-driven/> <!--打开注解扫描--> <context:component-scan base-package="com.xxzg.controller"/> <!--当web.xml过滤完路径之后,先到Dispatherservlet(前端控制器),通过框架的相关配置 调用HandlerMapping(处理器映射器),会生成一个controller,然后Dispatherservler调用handleradapter, 将已经注册过的controller和这个请求的controller进行匹配,这样就能够调用对应的方法,执行完controller返回 modleandview对象给Dispatherservlet,Dispatherservlet再调用视图解析器返回view,Diapatherservlet再对视图 进行渲染,也就是解析数据--> <!--处理器映射器,作用:通过对前端请求进行处理,映射出能够被识别的controller--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!--处理器适配器,作用:对写的controller层进行注册--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!--配置mvc视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--下边两个参数是访问链接的前缀和后缀 当web.xml中进行过滤过请求路径之后,会在controller中的@requestMapping()中匹配方法,然后符合的方法就会 将modelandview对象中的视图名称传过来进行拼接,然后就会到对应的页面--> <property name="prefix" value="/view/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
4、打印日志的配置文件
#定义log输出级别 log4j.rootLogger=DEBUG,Console,File #定义日志输出目的地为控制台 log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.Target=System.out #可以灵活地指定日志输出格式,下面一行是指定具体的格式 log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n #文件大小到达指定尺寸的时候产生一个新的文件 log4j.appender.File=org.apache.log4j.RollingFileAppender #指定输出目录,这个配置的输出路径在tomcat的bin目录下 log4j.appender.File.File=logs/ssm.log #定义文件最大大小 log4j.appender.File.MaxFileSize=10MB #输出所有日志,如果换成DEBUG表示输出DEBUG以上级别日志 log4j.appender.File.Threshold=ALL log4j.appender.File.layout=org.apache.log4j.PatternLayout log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n #显示sql命令 #mybatis显示SQL语句日志配置 #log4j.logger.org.mybatis=DEBUG log4j.logger.com.xxzg.dao=DEBUG
5、dao层的映射文件(Mapper.xml),为了方便查看效果,本文该文件放在dao层文件旁边
java文件:
com.xxzg.
1、entity层:
package com.xxzg.entity; public class Person { private int id; private String name; private int age; private String sex; private int depid; public Person() { } public Person(int id, String name, int age, String sex, int depid) { this.id = id; this.name = name; this.age = age; this.sex = sex; this.depid = depid; } public Person(String name, int age, String sex, int depid) { this.name = name; this.age = age; this.sex = sex; this.depid = depid; } 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 getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getDepid() { return depid; } public void setDepid(int depid) { this.depid = depid; } @Override public String toString() { return "Person{" + ", id='" + id + '\'' + ", name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + ", depid=" + depid + '}'; } }
2、dao层:
package com.xxzg.dao; import com.xxzg.entity.Person; import org.springframework.stereotype.Repository; import java.util.List; //注解作用:声明为bean对象,并交给spring容器管理 @Repository public interface Person_dao { int pinsert(Person person); int pdelete(Person person); int pupdate(Person person); List<Person> pquery(Person person); }
dao层对应的mabits映射文件:
<?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.xxzg.dao.Person_dao"> <!--<resultMap id="personresultMap" type="Person" autoMapping="true">--> <!--<id property="id" column="id"/>--> <!--<result property="name" column="name"/>--> <!--<result property="age" column="age"/>--> <!--<result property="sex" column="sex"/>--> <!--<result property="depid" column="depid"/>--> <!--</resultMap>--> <select id="pquery" resultType="Person"> select * from person </select> <insert id="pinsert"> insert into person(name,age,sex,depid) values (#{name},#{age},#{sex},#{depid}) </insert> <update id="pupdate"> </update> <delete id="pdelete"> </delete> </mapper>
3、service层:
package com.xxzg.service; import com.xxzg.entity.Person; import java.util.List; public interface Person_service { int pinsert(Person person); int pdelete(Person person); int pupdate(Person person); List<Person> pquery(Person person); }
接口实现层:
package com.xxzg.service; import com.xxzg.dao.Person_dao; import com.xxzg.entity.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class Person_serviceimpl implements Person_service { @Autowired private Person_dao person_dao; @Override public int pinsert(Person person) { return this.person_dao.pinsert(person); } @Override public int pdelete(Person person) { return this.person_dao.pdelete(person); } @Override public int pupdate(Person person) { return this.person_dao.pdelete(person); } @Override public List<Person> pquery(Person person) { return this.person_dao.pquery(person); } }
4、controller层:
package com.xxzg.controller; import com.xxzg.entity.Person; import com.xxzg.service.Person_service; import com.xxzg.service.Person_serviceimpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; import java.util.Map; @Controller @RequestMapping("/person") public class personcontroller { @Autowired private Person_service person_service; @RequestMapping("/query") public String query(Map map){ List<Person> persons= person_service.pquery(null); map.put("person",persons); return "person_list"; } }
页面文件:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <body> <h2>Hello World!</h2> <a href="person/query">员工列表</a> </body> </html>
<%-- Created by IntelliJ IDEA. User: 21933 Date: 2020/4/12 Time: 15:25 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>员工显示</title> </head> <body> <c:forEach items="${person}" var="p" varStatus="s"> <div>${p.name}-------${p.age}-------${p.sex}</div> </c:forEach> </body> </html>