![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c5269ed8cbd83030185df7d05baba6d7.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/36ba7bc20da5c6595567ddb07e2831cf.png)
db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/energycar?serverTimezone=GMT&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
springcontext
<?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:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean>
<!-- 配置Mapper扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置Mapper扫描包 -->
<property name="basePackage" value="cn.cetc.ssm.mapper" />
</bean>
<!-- 注解事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解 -->
<tx:annotation-driven/>
</beans>
springmvc
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置controller扫描包 -->
<context:component-scan base-package="cn.cetc.ssm.controller"/>
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置逻辑视图的前缀 -->
<property name="prefix" value="/WEB-INF/page/" />
<!-- 配置逻辑视图的后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 访问静态资源文件 -->
<!-- 文件若放在静态路径/statics下,可以直接访问,而不经过controller -->
<mvc:resources mapping="statics/**" location="statics/"/>
<!-- 配置servicer扫描包 -->
<context:component-scan base-package="cn.cetc.ssm.service"/>
</beans>
sqlMapConfig
<?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>
</configuration>
UserMapper.xml
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e79b37a62801bb6598091a53af7cc494.png)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.cetc.ssm.mapper.UserMapper">
<resultMap type="cn.cetc.ssm.pojo.User" id="user">
<id column="id" property="id" />
<result column="username" property="username" />
<result column="password" property="password" />
<result column="createtime" property="createtime" />
<result column="nickname" property="nickname" />
<result column="birthdate" property="birthdate" />
<result column="sex" property="sex" />
<result column="dltime" property="dltime" />
<result column="hpic" property="hpic" />
<result column="usertype" property="usertype" />
</resultMap>
<select id="getUserByUsername" parameterType="java.lang.String"
resultType="cn.cetc.ssm.pojo.User">
select * from user where username = #{username}
</select>
<insert id="saveUser" parameterType="cn.cetc.ssm.pojo.User">
insert into user(id,username,password,createtime,nickname,birthdate,sex,dltime,hpic,usertype)
values(#{id},#{username},#{password},#{createtime},#{nickname},#{birthdate},#{sex},#{dltime},#{hpic},#{usertype})
</insert>
</mapper>