JavaEE(SSM框架,黑马程序员) P193~P201 完结
Posted Scean周
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaEE(SSM框架,黑马程序员) P193~P201 完结相关的知识,希望对你有一定的参考价值。
一、SSM框架整合
1.1 原始方式整合
1.准备工作
2.创建Maven工程
3.导入Maven坐标
<!--spring相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--servlet和jsp-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<!--mybatis相关-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
4.编写实体类
public class Account
private int id;
private String name;
private double money;
//省略getter和setter方法
5.编写Mapper接口
public interface AccountMapper
//保存账户数据
void save(Account account);
//查询账户数据
List<Account> findAll();
6.编写Service接口
public interface AccountService
void save(Account account); //保存账户数据
List<Account> findAll(); //查询账户数据
7.编写Service接口实现
@Service("accountService")
public class AccountServiceImpl implements AccountService
public void save(Account account)
SqlSession sqlSession = MyBatisUtils.openSession();
AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
accountMapper.save(account);
sqlSession.commit();
sqlSession.close();
public List<Account> findAll()
SqlSession sqlSession = MyBatisUtils.openSession();
AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
return accountMapper.findAll();
MyBatisUtils.java
public class MyBatisUtils
public static SqlSession openSession() throws IOException
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
8.编写Controller
@Controller
@RequestMapping("/account")
public class AccountController
@Autowired
private AccountService accountService;
//保存
//produces = "text/html;charset=UTF-8" 解决中文乱码
@RequestMapping("/save",produces = "text/html;charset=UTF-8")
@ResponseBody
public String save(Account account)
accountService.save(account);
return "保存成功";
//查询
@RequestMapping("/findAll")
public ModelAndView findAll()
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("accountList");
modelAndView.addObject("accountList",accountService.findAll());
return modelAndView;
9.编写添加页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>保存账户信息表单</h1>
<form action="$pageContext.request.contextPath/account/save" method="post">
用户名称<input type="text" name="name"><br/>
账户金额<input type="text" name="money"><br/>
<input type="submit" value="保存"><br/>
</form>
</body>
</html>
10.编写列表页面
<table border="1">
<tr>
<th>账户id</th>
<th>账户名称</th>
<th>账户金额</th>
</tr>
<c:forEach items="$accountList" var="account">
<tr>
<td>$account.id</td>
<td>$account.name</td>
<td>$account.money</td>
</tr>
</c:forEach>
</table>
11.编写相应配置文件
•Spring配置文件:applicationContext.xml
<!-- 组件扫描 扫描service和mapper-->
<context:component-scan base-package="com.itheima">
<!-- 排除controller的扫描,主要在spring-mvc配置-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
•SprngMVC配置文件:spring-mvc.xml
<!-- 1、mvc注解驱动-->
<mvc:annotation-driven/>
<!-- 2、配置内部资源视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀-->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!-- 后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 3、开放静态资源权限-->
<mvc:default-servlet-handler/>
<!-- 4、注解的组件扫描 主要扫描controller-->
<context:component-scan base-package="com.itheima.controller">
</context:component-scan>
•MyBatis映射文件:AccountMapper.xml
<?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="com.itheima.mapper.AccountMapper">
<insert id="save" parameterType="account">
insert into account
values (#id, #name, #money)
</insert>
<select id="findAll" resultType="account">
select *
from account
</select>
</mapper>
•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>
<!-- 加载外部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 name="url" value="$jdbc.url"/>
<property name="username" value="$jdbc.user"/>
<property name="password" value="$jdbc.password"/>
</dataSource>
</environment>
</environments>
<!-- 加载映射文件-->
<mappers>
<!-- <mapper resource="com\\itheima\\mapper\\AccountMapper.xml"></mapper>-->
<package name="com.itheima.mapper"/>
</mappers>
</configuration>
•数据库连接信息文件:jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.user=root
jdbc.password=123456
•Web.xml文件:web.xml
<?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_4_0.xsd"
version="4.0">
<!-- 配置Spring监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 全局的初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- SpringMVC的前端控制器-->
<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>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</以上是关于JavaEE(SSM框架,黑马程序员) P193~P201 完结的主要内容,如果未能解决你的问题,请参考以下文章