| 1.创建一个maven工程,导入jar包,pom.xml的配置如下: |
| |
| <dependencies> |
| <dependency> |
| <groupId>junit</groupId> |
| <artifactId>junit</artifactId> |
| <version>4.12</version> |
| <scope>test</scope> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-context</artifactId> |
| <version>5.0.6.RELEASE</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-web</artifactId> |
| <version>5.0.6.RELEASE</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-webmvc</artifactId> |
| <version>5.0.6.RELEASE</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-tx</artifactId> |
| <version>5.0.6.RELEASE</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-aspects</artifactId> |
| <version>5.0.6.RELEASE</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-orm</artifactId> |
| <version>5.0.6.RELEASE</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>javax.servlet</groupId> |
| <artifactId>javax.servlet-api</artifactId> |
| <version>3.1.0</version> |
| <scope>provided</scope> |
| </dependency> |
| |
| <dependency> |
| <groupId>javax.servlet.jsp</groupId> |
| <artifactId>jsp-api</artifactId> |
| <version>2.2</version> |
| <scope>provided</scope> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.slf4j</groupId> |
| <artifactId>slf4j-log4j12</artifactId> |
| <version>1.7.25</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>commons-logging</groupId> |
| <artifactId>commons-logging</artifactId> |
| <version>1.2</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.apache.taglibs</groupId> |
| <artifactId>taglibs-standard-spec</artifactId> |
| <version>1.2.5</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.apache.taglibs</groupId> |
| <artifactId>taglibs-standard-impl</artifactId> |
| <version>1.2.5</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>mysql</groupId> |
| <artifactId>mysql-connector-java</artifactId> |
| <version>5.1.46</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>com.mchange</groupId> |
| <artifactId>c3p0</artifactId> |
| <version>0.9.5.2</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.mybatis</groupId> |
| <artifactId>mybatis</artifactId> |
| <version>3.4.6</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.mybatis</groupId> |
| <artifactId>mybatis-spring</artifactId> |
| <version>1.3.2</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.mybatis.caches</groupId> |
| <artifactId>mybatis-ehcache</artifactId> |
| <version>1.1.0</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>com.github.pagehelper</groupId> |
| <artifactId>pagehelper</artifactId> |
| <version>5.1.4</version> |
| </dependency> |
<!--逆向工程-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
| </dependencies> |
| <build> |
| <plugins> |
| <plugin> |
| <groupId>org.apache.felix</groupId> |
| <artifactId>maven-bundle-plugin</artifactId> |
| <extensions>true</extensions> |
| </plugin> |
<!--逆向工程-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<configuration>
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
| </plugins> </build> 2.在web.xml里做spring和springmvc的初始配置 <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> |
| |
| 3.SpringMVC只扫描Controller和ControllerAdvice: |
| |
| |
| <context:component-scan base-package="cn.ybzy.ssmweb" use-default-filters="false"> |
| <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> |
| <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> |
| </context:component-scan> |
| <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> |
| <property name="prefix" value="/WEB-INF/jsp/"></property> |
| <property name="suffix" value=".jsp"></property> |
| </bean> |
| <mvc:annotation-driven></mvc:annotation-driven> |
| <mvc:default-servlet-handler/> |
| |
| 4.Spring排除扫描Controller和ControllerAdvice: |
| |
| <context:component-scan base-package="com.hy.springmvc"> |
| <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> |
| <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> |
| </context:component-scan> |
| |
| |
| 5.jdbc.properties: |
| jdbc.user=root |
| jdbc.password=root |
| jdbc.driverClass=com.mysql.jdbc.Driver |
| jdbc.jdbcUrl=jdbc:mysql://localhost:3306/mybatisdemo??characterEncoding=UTF-8 |
| |
| jdbc.acquireIncrement=10 |
| jdbc.initialPoolSize=30 |
| jdbc.minPoolSize=5 |
| jdbc.maxPoolSize=40 |
| jdbc.maxStatements=1000 |
| jdbc.maxStatementsPerConnection=100 |
| |
| |
| 6.Spring的配置: |
| <context:property-placeholder location="classpath:jdbc.properties"/> |
| |
| <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> |
| <property name="user" value="${jdbc.user}"></property> |
| <property name="password" value="${jdbc.password}"></property> |
| <property name="driverClass" value="${jdbc.driverClass}"></property> |
| <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> |
| <property name="acquireIncrement" value="${jdbc.acquireIncrement}"></property> |
| <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property> |
| <property name="minPoolSize" value="${jdbc.minPoolSize}"></property> |
| <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> |
| <property name="maxStatements" value="${jdbc.maxStatements}"></property> |
| <property name="maxStatementsPerConnection" value="${jdbc.maxStatementsPerConnection}"></property> |
| </bean> |
| |
| |
| |
| <bean id="transactionManager" |
| class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> |
| <property name="dataSource" ref="dataSource"></property> |
| </bean> |
| |
| |
| |
| <tx:advice id="txAdvice" transaction-manager="transactionManager"> |
| <tx:attributes> |
| <tx:method name="get*" read-only="true"/> |
| <tx:method name="load*" read-only="true"/> |
| <tx:method name="find*" read-only="true"/> |
| <tx:method name="select*" read-only="true"/> |
| <tx:method name="*" read-only="false"/> |
| </tx:attributes> |
| </tx:advice> |
| |
| |
| <aop:config> |
| <aop:pointcut expression="execution(* cn.ybzy.springdemo.service.*.*(..))" |
| id="txPointcut"/> |
| <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> |
| </aop:config> |
| |
| |
| <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> |
| <property name="dataSource" ref="dataSource"></property> |
| <property name="configLocation" value="classpath:mybatis-config.xml"></property> |
| <property name="mapperLocations" value="classpath:cn/ybzy/ssmweb/dao/*.xml"></property> |
| </bean> |
| |
| |
| |
| <bean id="mapperScannerConfig" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> |
| <property name="basePackage" value="only.mxb.keshe.dao" /> |
| <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> |
| </bean> |
| |
| |
| 7.log4j.properties: |
| log4j.rootLogger = debug,stdout, D |
| log4j.appender.stdout = org.apache.log4j.ConsoleAppender |
| log4j.appender.stdout.Target = System.out |
| log4j.appender.stdout.Threshold = INFO |
| log4j.appender.stdout.layout = org.apache.log4j.PatternLayout |
| log4j.appender.stdout.layout.ConversionPattern=%d %p %m%n |
| log4j.appender.D = org.apache.log4j.DailyRollingFileAppender |
| log4j.appender.D.File = ./log4j.log |
| log4j.appender.D.Append = true |
| log4j.appender.D.Threshold = DEBUG |
| log4j.appender.D.layout = org.apache.log4j.PatternLayout |
| log4j.appender.D.layout.ConversionPattern=%d %p %m%n |
| |
| |
| 9.逆向工程: |
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:\About-Java\apache-maven-3.6.0\repository\mysql\mysql-connector-java\5.1.37\mysql-connector-java-5.1.37.jar"/>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!--是否去除自动生成的注释 true是:false 否-->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/xiaomissm?characterEncoding=UTF-8" userId="root" password="123456">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--targetPackage目标包,生成实体类的位置-->
<javaModelGenerator targetPackage="only.mxb.traindemo.pojo" targetProject="src/main/java">
<!--enableSubPackages,是否让schema作为包的后缀-->
<property name="enableSubPackages" value="false"/>
<!--从数据库返回的值被清除前后空格-->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--targetProject:mapper映射文件生成的位置-->
<sqlMapGenerator targetPackage="only.mxb.traindemo.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false"></property>
</sqlMapGenerator>
<!--targetPackage:mapper接口生成的位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="only.mxb.traindemo.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!--指定数据库表,要和数据库中进行对应,否则将会出错-->
<table tableName="address"></table>
<!-- enableCountByExample="false" enableUpdateByExample="false"-->
<!-- enableDeleteByExample="false" enableSelectByExample="false"-->
<!-- selectByExampleQueryId="false"-->
<table tableName="admin"></table>
<table tableName="carshop"></table>
<table tableName="orderdetail"></table>
<table tableName="product_info"
></table>
<table tableName="product_type"
></table>
<table tableName="users"
></table>
<table tableName="xmorder"
></table>
</context>
</generatorConfiguration>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· SQL Server统计信息更新会被阻塞或引起会话阻塞吗?
· 传国玉玺易主,ai.com竟然跳转到国产AI
· 本地部署 DeepSeek:小白也能轻松搞定!
· 自己如何在本地电脑从零搭建DeepSeek!手把手教学,快来看看! (建议收藏)
· 我们是如何解决abp身上的几个痛点
· 普通人也能轻松掌握的20个DeepSeek高频提示词(2025版)