首先需要在eclipse market上安装maven;可以选择安装附带maven的eclipse jee。
maven需要进行一点设置,包括默认jdk版本设置,以及启用阿里云镜像库避免下载不了jar包。
打开工具栏windows - properties 找到maven - user setting ,打开设置文件的目录,编辑设置文件,如果没有设置文件,可以自己新建一个。
打开setting.xml,贴入阿里镜像仓库
<mirrors>
<mirror>
<id>alimavenid>
<name>aliyun mavenname>
<url>
http://maven.aliyun.com/nexus/content/groups/public/
url>
<mirrorOf>centralmirrorOf>
mirror>
mirrors>
设置默认jdk版本
<profiles>
<profile>
<id>jdk-1.8id>
<activation>
<activeByDefault>trueactiveByDefault>
<jdk>1.8jdk>
activation>
<properties>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<maven.compiler.compilerVersion>1.8maven.compiler.compilerVersion>
properties>
profile>
profiles>
可以到此网站下载对应版本https://spring.io/tools/sts/all;或者在线安装都是可以,目前eclipse4.10.0的适配版本依然是3.9.7 Spring Tools3
new 一个新的maven项目,勾选create a simple project()
next
点***finish***,之后可以得到项目结构如下
接下来删除一些没用的动态项目结构,以及生成我们网站开发需要的包和结构。
在项目上右键 - > properties
取消动态网站项目的勾选点apply,
之后再次勾选上该项,下面会有
Further configuration available,
这样,项目结构就是我们所需用的比较合适的了。
之后开始新建项目的java包,例如dao包
其他所需用的包建立方法一致。如,bean, utils,test, service,controller,等等吧。
当eclipse开发环境设置完成,下面就开始新建maven项目,进入ssm项目的配置环节。
<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.0modelVersion>
<groupId>edu.xuptgroupId>
<artifactId>MedicalAuxiliaryartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.1.3.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.1.3.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.1.3.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>5.1.3.RELEASEversion>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.3version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.0version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.13version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-coreartifactId>
<version>1.3.7version>
dependency>
dependencies>
project>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>dispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>dispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<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>isForceRequestEncodingparam-name>
<param-value>trueparam-value>
init-param>
<init-param>
<param-name>isForceResponseEncodingparam-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>
<init-param>
<param-name>param-name>
<param-value>param-value>
init-param>
filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
在项目webapp目录下新建views目录,用来存放视图页面,新建 dispatcherServlet-servlet.xml文件配置SpringMVC。
<context:component-scan base-package="com.csdn.test" use-default-filters="false">
<context:exclude-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>
在项目的 src/main/resources目录下,新建dbconfig.properties,数据库设置文件,applicationContext.xml spring配置文件,mybatis-config,xml mybatis配置文件,
applicationContext.xml文件如下
<context:component-scan base-package="com.csdn.test">
<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 id="MapperScanner"
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.csdn.test.dao">property>
bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="pooledDataSource">property>
bean>
<aop:config>
<aop:pointcut
expression="excution(*com.csdn.test.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>
进入mybatis官方网站http://www.mybatis.org/mybatis-3/getting-started.html找到官方文档,选择GettingStarted ,第一项是在pom文件中增加依赖,下面就是Building SqlSessionFactory from XML,配置文件的设置,首先将文件头拷贝过来。
<configuration>
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
settings>
<typeAliases>
<package name="com.csdn.test.bean"/>
typeAliases>
configuration>
接下来就是mapper文件和dao接口对应的文件。
逆向工程生成代码,不需要手动一个个去敲bean类和dao类,以及mapper.xml数据库映射文件。
使用mybatis逆向工程生成bean和对应的mapper文件
搜索找到mybatis generator 官方文档http://www.mybatis.org/generator/quickstart.html
Quick Start Guide -->
See the XML Configuration File Reference page for an example of a configuration file.查看示例的配置xml文件
在项目最外层,建立mbg.xml文件,把内容贴进去。
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
commentGenerator>
generatorConfiguration>
<jdbcConnection driverClass="com.mysql.jc.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/shujukuming?"
userId="root" password="">
jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
javaTypeResolver>
<javaModelGenerator
targetPackage="edu.xupt.medicalauxiliary.bean"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
javaModelGenerator>
<sqlMapGenerator targetPackage="mapper"
targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="edu.xupt.medicalauxiliary.dao"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
javaClientGenerator>
<table tableName="ma_user" domainObjectName="User">table>
<table tableName="ma_utype" domainObjectName="UserType">table>
context>
在官方文档中找到Running MyBatis Generator – > with java
在项目的test目录下,新建MGBTest类
public class MGBTest {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mgb.xml"); //修改mybatis的设置文件名
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);
//这里需要注意的是,如果数据库连接出现问题,需要修改mgb.xml文件,而不是dbconfig设置文件
}
}
初步的配置到此结束。