项目搭建的步骤
在eclipse中创建web项目,并导入相关jar包
创建分层的包和配置文件结构
创建项目需要的数据库的表,并用mybatis反向工厂创建dao层,dao层实现等文件,pojo类以及mapper的xml文件
完善service层,controller层
配置各个配置文件
需要用到的相关jar包
项目分层结构
src下:
entity层:实体类,或者pojo
dao层:操作数据
service层
serviceimpl层:service层的实现
controller层
util层:工具类,减少代码冗余、解耦合
test层:测试层
resource下:
spring:包含spring相关的配置文件
spring-dao.xml:dao层配置文件
spring-mvc.xml:mvc配置文件
spring-service.xml:service层配置文件
mapper:包含mybatis的映射文件
mybatis-config.xml:mybatis的主配置文件
generatorConfig.xml:用于mybatis反向工厂生成实体类一级mapper配置文件的配置文件
WebContent下:
css:css样式文件
js:JavaScript文件
WEB-INF
lib:用到的jar包
view:jsp文件存放在这里
web.xml
welcome.jsp
利用Mybatis反向工厂自动生成相关文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorConfiguration> <!-- help marketplace(eclipse插件市场) 搜索mybatis generator 点击install 如果过程中弹框出现认证权限的文件直接点ok继续安装重启eclipse 选中generatorConfig.xml右键run as查看是否有mybatis运行选项 --> <!-- jdbc的类路径(最好是绝对路径,能找到就行,不一定要是项目里的jar包) --> <classPathEntry location="xxx\mysql-connector-java-5.1.15-bin.jar" ></classPathEntry> <context id="context1"> <!-- 不生成注释 --> <commentGenerator> <property name="suppressAllComments" value="true"></property> </commentGenerator> <!-- 连接数据库的四大元素 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="qweqwe" /> <!-- 生成java实体类(pojo、model层) targetPackage:指定生成的包路径 targetProject:指定项目名 --> <javaModelGenerator targetPackage="com.yunke.entity" targetProject="BookShop" /> <!-- 生成映射文件路径 --> <sqlMapGenerator targetPackage="com.yunke.dao" targetProject="BookShop" /> <!-- 生成接口路径--> <javaClientGenerator targetPackage="com.yunke.dao" targetProject="BookShop" type="XMLMAPPER" /> <!-- schema:数据库 tableName:表名 --> <table schema="" tableName="book" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
Mybatis主配置文件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="" value=/> </settings> --> <!-- <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="qweqwe"/> </dataSource> </environment> </environments> --> <mappers> <!-- <mapper resource="org/mybatis/example/BlogMapper.xml"/> --> </mappers> </configuration>
spring-dao.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="qweqwe"/> </bean> <!-- 注入SqlSessionFactory对象 --> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据源对象属性 --> <property name="dataSource" ref="dataSource"></property> <!-- 注入mapper映射路径的属性 --> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> <!-- 注入mybatis配置文件路径属性 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- 3、注册dao层 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定sqlSessionFactoryBeanName的值,用于创建dao对象 --> <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property> <!-- 指定扫描注册的dao所在的包 --> <property name="basePackage" value="com.yunke.dao"></property> </bean> </beans>
spring-mvc.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 开启扫描 --> <context:component-scan base-package="com.yunke.controller"></context:component-scan> <!-- 过滤静态资源 --> <mvc:annotation-driven></mvc:annotation-driven> <mvc:default-servlet-handler /> <!-- <mvc:resources location="/css/" mapping="/css/**"></mvc:resources> <mvc:resources location="/js/" mapping="/js/**"></mvc:resources> --> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀名 --> <property name="prefix" value="/WEB-INF/view/"></property> <!-- 后缀名 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
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"> <welcome-file-list> <welcome-file>welcome.jsp</welcome-file> </welcome-file-list> <!-- 指定spring的配置文件(当不使用默认配置文件名和路径的时候) --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-dao.xml</param-value> </context-param> <!-- 添加监听器,去加载spring的配置文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置前置控制器DispatcherServlet,去拦截所有的请求统一分配 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 如果不使用springmvc的默认文件配置名和路径则在此声明使用的springmvc的配置文件 同时可以与spring的配置文件一起加载 --> <!-- 一起加载 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </init-param> <!-- 只加载spirngmvc --> <!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 过滤器,防止乱码 --> <filter> <filter-name>Encoding</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>Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>