首先我们需要创建一个maven项目
1.右键 new maven project
第一个勾选然后下一步
groupid 可以写com.xxx.xxx 之类的
artifaceid 写项目名 xxx
web项目选择打war包 然后下一步就ok
2.项目中少web.xml /WEB-INFO 右键项目 properties → project faces → Dynamic Web Module 勾选去掉点击apply →再勾选上 然后 它会提醒你配置 然后第二个框目录 写src/main/webapp 应用就好了 maven的setting 路径自己配置 镜像位置推荐用阿里云的
3.pom文件的配置
这里jar包miss报错 还有项目红色叹号 怎么弄maven update project 还有build path clean都没有用, 直接去网址下载jar包 然后放进文件夹里
具体的pom 需要的jar包 如下
- <dependencies>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>4.3.7.RELEASEversion>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>4.3.7.RELEASEversion>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-aspectsartifactId>
- <version>4.3.7.RELEASEversion>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.4.2version>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>1.3.1version>
- dependency>
-
- <dependency>
- <groupId>c3p0groupId>
- <artifactId>c3p0artifactId>
- <version>0.9.1.2version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.41version>
- dependency>
-
- <dependency>
- <groupId>jstlgroupId>
- <artifactId>jstlartifactId>
- <version>1.2version>
- dependency>
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>servlet-apiartifactId>
- <version>2.5version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- <scope>testscope>
- dependency>
-
-
-
- dependencies>
4.我们对WEB-INF 下的web.xml 进行编写
1).spring 的容器 ContextLoaderListener 快捷键补全 alt+/
只用修改这个 并且在src/main/resources 右键 new spring bean configuration File 创建上面的 spring的配置文件
2).springMVC前端控制器
DispatcherServlet 快捷键提示补全
将
然后在web.xml同级目录下创建dispatcherServlet-servlet.xml 创建方式同上applicationContext.xml
在这个里面控制springMVC扫描
3).添加两个基本的过滤器
- <filter>
- <filter-name>CharacterEncodingFilterfilter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
- <init-param>
- <param-name>encodingparam-name>
- <param-value>utf-8param-value>
- init-param>
- <init-param>
- <param-name>forceRequestEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
- <init-param>
- <param-name>forceResponseEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
-
- <filter>
- <filter-name>HiddenHttpMethodFilterfilter-name>
- <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
- filter>
- <filter-mapping>
- <filter-name>HiddenHttpMethodFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
5.在dispatcherServlet-servlet.xml配置文件顶部加这个
xmlns:context="http://www.springframework.org/schema/context"
然后对SpringMVC 的配置文件 包含网站跳转逻辑的控制,配置 即注解的扫描的范围的指定
选择该xml下面的Namespaces mvc勾选上
- <context:component-scan base-package="com.ice" use-default-filters="false">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- context:component-scan>
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/views/">property>
- <property name="suffix" value=".jsp">property>
- bean>
-
-
- <mvc:default-servlet-handler/>
- <mvc:annotation-driven>
-
- mvc:annotation-driven>
6. 配置c3p0数据库相关
在applicationContext.xml 文件打开 然后选择namespace aop(√) context (√) tx(√)
-
- <context:component-scan base-package="com.ice">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- context:component-scan>
-
- <context:property-placeholder location="classpath:dbconfig.properties"/>
- <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="jdbcUrl" value="${jdbc.jdbcUrl}">property>
- <property name="driverClass" value="${jdbc.driverClass}">property>
- <property name="user" value="${jdbc.user}">property>
- <property name="password" value="${jdbc.password}">property>
- bean>
-
-
- <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-
- <property name="configLocation" value="classpath:mybatis-config.xml">property>
-
- <property name="dataSource" ref="pooledDataSource">property>
-
- <property name="mapperLocations" value="classpath:mapper/*.xml">property>
- bean>
-
-
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
-
- <property name="basePackage" value="com.ice.dao">property>
-
-
-
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="pooledDataSource">property>
- bean>
-
- <aop:config>
-
- <aop:pointcut expression="execution(* com.ice.service..*(..))" id="txPoint"/>
-
- <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
- aop:config>
-
- <tx:advice id="txAdvice" >
- <tx:attributes>
- <tx:method name="*"/>
- <tx:method name="get*" read-only="true"/>
- tx:attributes>
- tx:advice>
此步骤创建了 mapper包 同时还有mybatis的配置文件 mybatis-config.xml 与applicationContext在同一目录下
7.mybatis的配置 mybatis-config.xml的主要配置如下
- "1.0" encoding="UTF-8"?>
- configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
-
- <configuration>
- <settings>
- <setting name="mapUnderscoreToCamelCase" value="true"/>
- settings>
- <typeAliases >
- <package name="com.ice.bean" />
- typeAliases>
- configuration>
然后就是 mybatis逆向工程 必须的xml 和java类
这个是xxx.xml 直接放在包下面 跟pom.xml同级
- "1.0" encoding="UTF-8"?>
- generatorConfiguration
- PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
- "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
- <generatorConfiguration>
-
-
- <context id="cosmetic" targetRuntime="MyBatis3">
- <commentGenerator>
- <property name="suppressDate" value="true"/>
-
- <property name="suppressAllComments" value="true"/>
- commentGenerator>
-
-
-
- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.204.131:3306/ssh_crud" userId="root" password="123456">
- jdbcConnection>
-
-
- <javaTypeResolver>
- <property name="forceBigDecimals" value="false"/>
- javaTypeResolver>
-
-
- <javaModelGenerator targetPackage="com.ice.bean" targetProject=".\src\main\java">
- <property name="enableSubPackages" value="true"/>
- <property name="trimStrings" value="true"/>
- javaModelGenerator>
-
-
-
- <sqlMapGenerator targetPackage="com.ice.dao" targetProject=".\src\main\java">
- <property name="enableSubPackages" value="true"/>
- sqlMapGenerator>
-
-
-
- <javaClientGenerator type="XMLMAPPER" targetPackage="com.ice.dao" targetProject=".\src\main\java">
- <property name="enableSubPackages" value="true"/>
- javaClientGenerator>
-
-
-
- <table tableName="emp" domainObjectName="Employee" />
- <table tableName="dept" domainObjectName="Department" />
-
- context>
- generatorConfiguration>
xxx.java
- package com.ice.test;
-
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
-
- import org.mybatis.generator.api.MyBatisGenerator;
- import org.mybatis.generator.config.Configuration;
- import org.mybatis.generator.config.xml.ConfigurationParser;
- import org.mybatis.generator.internal.DefaultShellCallback;
-
- public class MBGTest {
-
- //生成bean mapper 的方法
-
- public void generator() throws Exception{
-
- List
warnings = new ArrayList(); - boolean overwrite = true;
- //指定 逆向工程配置文件
- File configFile = new File("mbg.xml");
- ConfigurationParser cp = new ConfigurationParser(warnings);
- Configuration config = cp.parseConfiguration(configFile);
- DefaultShellCallback callback = new DefaultShellCallback(overwrite);
- MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
- callback, warnings);
- myBatisGenerator.generate(null);
-
- }
- public static void main(String[] args) throws Exception {
- try {
- MBGTest generatorSqlmap = new MBGTest();
- generatorSqlmap.generator();
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
-
-
- }
运行上面的java 就生成了所需的 mapper 以及实体类javabean
我们的框架三部分就完成的差不多了