用SpringMVC+Mybatis+Spring搭建框架的简单学习

第一步:添加jar包,如下图



第二步:配置web.xml,注意springmvc-servlet.xml配置文件。关键代码如下

<servlet>
  <servlet-name>springmvc</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>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>*.do</url-pattern>

  </servlet-mapping>


第三步:配置springmvc-servlet.xml,位置在src下。代码如下

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">


<!-- 扫描包-->
<context:component-scan
base-package="org.lmz.controller" />
<context:component-scan
base-package="org.lmz.service" />
<context:component-scan base-package="org.lmz.dao" />


<!-- 视图 解析器-->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp"></property>
</bean>


<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.cj.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/db_online_learning?serverTimezone=UTC&amp;useSSL=false" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>


<!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mappers.xml文件 -->
<property name="mapperLocations"
value="classpath:org/lmz/mapping/*.xml"></property>
<!-- mybatis配置文件 <property name="configLocation" value="classpath:mybatis-config.xml"></property> -->
</bean>


<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.lmz.dao" />
<property name="sqlSessionFactoryBeanName"
value="sqlSessionFactory"></property>
</bean>


</beans>


第四步:用GeneratorTool.java和generatorConfig.xml生成,在org.lmz.dao、org.lmz.entity和org.lmz.mapping包下的文件,关键的jar为mybatis-3.2.6.jar、mybatis-generator-core-1.3.5.jar和mybatis-spring-1.2.2.jar。代码如下:

 GeneratorTool.java的关键代码:

private static void shell() {

        List<String> warnings = new ArrayList<String>();
        try {
        String path="/src/generatorConfig.xml";
            String configFilePath = System.getProperty("user.dir").concat(path);
            System.out.println("加载配置文件===" + configFilePath);
            boolean overwrite = true;
            File configFile = new File(configFilePath);
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(configFile);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            ProgressCallback progressCallback = new VerboseProgressCallback();
            myBatisGenerator.generate(progressCallback);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        for (String warning : warnings) {
            System.out.println("【warning】"+warning);
        }
    }

generatorConfig.xml的关键代码:

<?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>
<!-- 数据库驱动 -->
<classPathEntry
location="D:\4Jar包\mysql-connector-java-8.0.11.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">

<!-- 生成的pojo,将implements Serializable-->    
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> 

<commentGenerator>
<property name="suppressDate" value="true" />    <!-- 是否取消注释 -->
<!-- 是否去除自动生成的注释 true:是 : false:否 -->     <!-- 是否生成注释代时间戳 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库链接URL,用户名、密码   特殊符号&后面有加上amp;-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/db_online_learning?serverTimezone=UTC&amp;useSSL=false" 
userId="root"
password="password">
</jdbcConnection>


<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>

<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="org.lmz.entity"
targetProject="src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="org.lmz.mapping"
targetProject="src">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="org.lmz.dao" targetProject="src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 -->
<table tableName="sysadmin" domainObjectName="SysAdmin"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>


</context>

</generatorConfiguration>


第五步:创建未完成的class文件。


对XxController后台控制器,关键代码截图:



对XxService服务接口,关键代码截图:



对Xx服务实现类,关键代码截图:



第六步:jsp创建连接测试。关键代码如下:

<body>
WebContent下的主页
<%
String uri=request.getContextPath();
%>


<a href="<%=uri%>/SysAdminController/getSysAdminServiceList.do">获取数据的测试</a>

</body>


对springmvc的简单配置完成了,祝你好运!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值