标签:ssm ogg rtu default inf xsd ali loader viso
1.ioc组件扫描,扫描service
2.加载properties文件
3.配置数据源信息
4.配置sqlSessionFactory工厂
5.扫描mapper所在的包,为mapper创建实现类
6.声明式事务控制
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--ioc组件扫描 扫描service-->
<context:component-scan base-package="service业务层包名"></context:component-scan>
<!--加载propeties文件-->
<context:property-placeholder location="classpath:jdbc.properties"> </context:property-placeholder>
<!--配置数据源信息-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置sqlsessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--定义别名-->
<property name="typeAliasesPackage" value="实体类包"></property>
<!--加载mybatis核心文件-->
</bean>
<!--扫描mapper所在的包 为mapper创建实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper接口的包"></property>
</bean>
<!--声明式事务控制-->
<!--平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--配置事务增强-->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!--事务的aop织入-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.ppvir.service.impl.*.*(..))"></aop:advisor>
</aop:config>
</beans>
1.组件扫描
2.配置mvc注解驱动
3.内部资源视图解析器
4.开发静态资源访问权限
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--组件扫描 只扫描controller-->
<context:component-scan base-package="controller层包"></context:component-scan>
<!--配置mvc注解驱动-->
<mvc-annotation-driven></mvc-annotation-driven>
<!--内部资源视图解析器-->
<bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--开放静态资源访问权限-->
<mvc:default-servlet-handler></mvc:defaule-servlet-handler>
</beans>
在spring配置文件中配置后可以省略。
<configuration>
<!--定义别名-->
<typeAliases>
<package name="domain实体类包">
</typeAliases>
</configuration>
1.spring 监听器
2.springMVC的前端控制器
3.乱码过滤器
<!--spring监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--springMVC的前端控制器dispatcherServlert本质是servlet-->
<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:spring-mvc.xml</param-value>
</init-param>
<!--启动加载-->
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--乱码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=root
#
# Hibernate, Relational Persistence for Idiomatic Java
#
# License: GNU Lesser General Public License (LGPL), version 2.1 or later.
# See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
#
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=debug, stdout
//mybatis注解
public interface UserMapper {
@Insert("insert into user(username,password)values(#{username},#{password})")
void save(User user);
@Select("select * from user")
List<User> findAll();
}
public interface IUserService {
void save(User user);
List<User> findAll();
}
@Service("userService")
@Transactional
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public void save(User user) {
userMapper.save(user);
}
@Override
public List<User> findAll() {
List<User> userList = userMapper.findAll();
return userList;
}
}
public class User {
private Integer id;
private String username;
private String email;
private String password;
private long phoneNum;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
//查询所有用户
@RequestMapping(value = "/findAll")
public ModelAndView findAll(){
List<User> userList = userService.findAll();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("userList",userList);
modelAndView.setViewName("userList");
return modelAndView;
}
//添加用户
@RequestMapping(value = "/save",produces = "text/html;charset=UTF-8")
@ResponseBody
public String save(User user){
userService.save(user);
return "userList";
}
}
<!--提交表单页面-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>添加用户信息</h1>
<form name="userForm" action="${pageContext.request.contextPath}/user/save" method="post">
用户名:<input name="username" type="text"><br/>
密码:<input name="password" type="password"><br/>
<input type="submit" value="保存">
</form>
</body>
</html>
<!--查询用户页面-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>用户列表</h1>
<table border="1px">
<tr>
<th>用户id</th>
<th>用户名</th>
<th>密码</th>
</tr>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
1.加载jdbc.properties配置文件
2.定义别名 package domain包名
3.配置数据源环境 environment
4.加载映射mappers package name=”映射接口所在包“
<configuration>
<!--加载properties文件-->
<properties resource="jdbc.properties"></properties>
<!--定义别名-->
<typeAliases>
<!--<typeAlias type="com.itheima.domain.Account" alias="account"></typeAlias>-->
<package name="com.itheima.domain"></package>
</typeAliases>
<!--环境-->
<environments default="developement">
<environment id="developement">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
</environment>
</environments>
<!--加载映射mapper-->
<mappers>
<package name="mapper接口包"></package>
</mappers>
</configuration>
<mapper namespace="mapper接口的全类名">
增删改语句
</mapper>
标签:ssm ogg rtu default inf xsd ali loader viso
原文地址:https://www.cnblogs.com/ppvir/p/11444671.html