config/mybatis目录下创建mybatis配置文件sqlMapConfig.xml
<?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>
<!--全局配置-->
<settings>
<!-- 打印日志 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--别名配置-->
<typeAliases>
<package name="com.xieli.liu.pojo"/>
</typeAliases>
</configuration>
config目录下创建db.properties文件
#加载驱动
mysql.driver=com.mysql.jdbc.Driver
#加载数据库
mysql.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
#用户名
mysql.user=root
#密码
mysql.password=123456
config目录下创建log4j.properties文件
# Global logging configuration
# developer-->DEBUG product-->INFO or ERROR
#开发环境DEBUG,生产环境info或error
#log4j.rootLogger=info, stdout
log4j.rootLogger=DEBUG, stdout
#log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
config/spring目录下创建application-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--加载数据库信息-->
<context:property-placeholder location="classpath:config/db.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.user}"/>
<property name="password" value="${mysql.password}"/>
</bean>
<!--SqlSessionFactory加入spring-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--configLocation属性指定mybatis的核心配置文件-->
<property name="configLocation" value="classpath:config/mybatis/sqlMapConfig.xml" />
<!--dataSource属性指定要用到的连接池-->
<property name="dataSource" ref="dataSource" />
</bean>
<!--mapper扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--适用sqlSessionFactory会使context:property-placeholder失效-->
<!--<property name="sqlSessionFactory" ref="sqlSessionFactory"/>-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!--如果有多个包,用半角逗号隔开-->
<property name="basePackage" value="com.xieli.liu.mapper " />
</bean>
</beans>
参考https://blog.csdn.net/weixin_45742032/article/details/103270655
package com.xieli.liu.service;
import com.xieli.liu.pojo.UserInfo;
import java.util.List;
/**
* Created by Administrator on 2019-11-27.
*/
public interface UserInfoService {
public List<UserInfo> selectUserList(UserInfo user);
}
package com.xieli.liu.service;
import com.xieli.liu.mapper.UserInfoMapper;
import com.xieli.liu.pojo.UserInfo;
import com.xieli.liu.pojo.UserInfoExample;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/**
* Created by Administrator on 2019-11-27.
*/
public class UserInfoServiceImpl implements UserInfoService {
@Autowired
UserInfoMapper UserInfoMapper;
@Override
public List<UserInfo> selectUserList(UserInfo user) {
UserInfoExample userInfoExample = new UserInfoExample();
//构建查询条件
UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
//用户名=maomao
criteria.andUserNameEqualTo(user.getUserName());
List<UserInfo> userInfos = UserInfoMapper.selectByExample(userInfoExample);
return userInfos;
}
}
在config/spring包下创建springService.xml,将UserInfoService加入spring中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userInfoService" class="com.xieli.liu.service.UserInfoServiceImpl"/>
</beans>
在config/spring包下创建spring-transaction.xml
可以引用spring配置文件中的数据源
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
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-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--加载数据库信息-->
<context:property-placeholder location="classpath:config/db.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.user}"/>
<property name="password" value="${mysql.password}"/>
</bean>
<!--事务管理器:对mybatis操作数据库进行事务控制,采用springjdbc的事务控制类-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 通知-->
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager" > <!-- 仍然使用txManager作为事务管理组件 -->
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/> <!-- 在哪些方法上添加事务管理 -->
<tx:method name="delete*" propagation="REQUIRED"/> <!-- 这里写方法名 -->
<tx:method name="insert*" propagation="REQUIRED"/> <!-- 支持通配符 -->
<tx:method name="updata*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 通过aop机制完成事务管理 -->
<aop:config>
<!-- 这个expression的写法有讲究 -->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.xieli..*Service.*(..))"/>
</aop:config>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 扫描组件 -->
<context:component-scan base-package="com.xieli.liu.controller"/>
<!--注解适配器和映射器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter "/>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>
package com.xieli.liu.controller;
import com.xieli.liu.pojo.UserInfo;
import com.xieli.liu.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
/**
* Created by Administrator on 2019-11-27.
*/
@Controller
public class UserInfoController {
@Autowired
UserInfoService userInfoService;
@RequestMapping("queryuser")
public ModelAndView queryuserss() throws Exception {
UserInfo userInfo = new UserInfo();
userInfo.setUserName("小绿");
List<UserInfo> userInfos = userInfoService.selectUserList(userInfo);
ModelAndView mv = new ModelAndView("WEB-INF/jsp/usermasseger.jsp");
mv.addObject("userInfo",userInfos.get(0));
return mv;
}
}
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019-11-23
Time: 14:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<table style="border: solid 1px">
<tr>
<td>
姓名
</td>
<td>
${userInfo.userName}
</td>
</tr>
<tr>
<td>
密码
</td>
<td>
${userInfo.userPwd}
</td>
</tr>
<tr>
<td>
昵称
</td>
<td>
${userInfo.nickName}
</td>
</tr>
</table>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--加载spring容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/spring/*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--spring前端控制-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--contextConfigLocation配置spring处理器,映射器适配器-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/springmvc/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
访问controller的URL路经http://localhost:8080/ssm/queryuser.action