之前学习的时候大多都是使用maven搭建,但今天想尝试一下不使用maven,但是配置文件都差不多,只不过不使用maven的不用去配置pom.xml,而是需要自己导入相应的包,废话不多说,直接开整。
1. 先看一下我的项目结构,我的项目就只是新建简单的java web项目
jdk:1.8
tomcat:8.5
mysql:5.5
2. 相关的依赖架包
注意:这里的架包最好版本号和spring的想依赖,否则,会出现一些版本不兼容的错误,我在搭的时候就有过,特此相告。
3、先把相关的文件建好
4、配置文件
- mybatis-config.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--日志-->
<settings>
<setting name="logImpl" value="LOG4J"/>
<!--自动映射级别 NONE PARTIAL FULL-->
<setting name="autoMappingBehavior" value="FULL"/>
<!--<setting name="cacheEnabled" value="true"/>-->
</settings>
<!--别名-->
<typeAliases>
<package name="com.hsx.pojo"/>
</typeAliases>
</configuration>
- 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: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-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--1、数据源-->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<!--2、数据源工厂-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--3、mapper扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hsx.mapper"/>
</bean>
<!--4、扫描service-->
<context:component-scan base-package="com.hsx.service"/><!--service接口-->
<context:component-scan base-package="com.hsx.service.impl"/><!--service实现-->
<!--事务-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
- springmvc-servlet.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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--1、controller扫描-->
<context:component-scan base-package="com.hsx.controller"/>
<!--2、注解驱动-->
<mvc:annotation-driven/>
<!--3、视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--4、静态资源加载-->
<mvc:resources mapping="/statics/**" location="/statics/"/>
</beans>
- 除此外还有个日志文件和连接数据库的文件,这两个不用改,只是数据库的改一下成自己的数据库,用户名,密码
log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
db.properties
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/你自己的数据库?useSSL=true&userUnicode=true&characterEncoding=utf-8
db.username=数据库用户名
db.password=数据库密码
5、配置web.xml
web.xml所有项目基本不变,只是里边的用到第4步配置文件的文件名改成自己对应的
<?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">
<!--1、applicationContext文件加载-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value><!--这里边是自己的context文件名,可以自己改-->
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--2、过滤器 post-->
<filter>
<filter-name>encode</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>encode</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--3、核心控制器-->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value><!--springMVC是配置文件,根据自己的名字可改-->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
测试
弄玩这些就基本没问题了,测试一下可不可行,在controller中写一个测试
@Controller
public Test{
@RequestMapping("/test")
public String test(){
return "test";//jsp文件
}
}
然后在WEB-INF/jsp/下建一个test.jsp,在里边随便敲一个字
运行项目,在浏览器里输入localhost:8080/你的项目名字/test
如果看见你在test.jsp中的输的字,就证明是成功了。