目录
第一章:搭建整合环境
1、引入坐标:
2、编写实体类,在ssm_domain项目中编写
3、 编写dao接口
4、编写service接口和实现类
第二章:Spring框架代码的编写
1. 搭建和测试Spring的开发环境
2. 在ssm_web项目中编写测试方法,进行测试
3.测试结果
第三章:Spring整合SpringMVC框架
1. 搭建和测试SpringMVC的开发环境
1.在web.xml中配置DispatcherServlet前端控制器
2.在web.xml中配置DispatcherServlet过滤器解决中文乱码
3. 创建springmvc.xml的配置文件,编写配置文件
4. 测试SpringMVC的框架搭建是否成功
5.测试结果
2. Spring整合SpringMVC的框架
1. 目的:在controller中能成功的调用service对象中的方法。
2. 在项目启动的时候,就去加载applicationContext.xml的配置文件,在web.xml中配置ContextLoaderListener监听器(该监听器只能加载WEB-INF目录下的applicationContext.xml的配置文件)。
3. 在controller中注入service对象,调用service对象的方法进行测试
4.测试结果
第四章:Spring整合MyBatis框架
1. 搭建和测试MyBatis的环境
1. 在web项目中编写SqlMapConfig.xml的配置文件,编写核心配置文件
2. 在AccountDao接口的方法上添加注解,编写SQL语句
3. 编写测试的方法
4.测试结果
2. Spring整合MyBatis框架
1. 目的:把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中
2. 在AccountDao接口中添加@Repository注解,在service中注入dao对象,进行测试
3.配置Spring的声明式事务管理
4.测试保存帐户的方法
5.测试结果
整个项目所包含的文件
UTF-8
1.8
1.8
5.0.2.RELEASE
1.6.6
1.2.12
5.1.6
3.4.5
org.aspectj
aspectjweaver
1.6.8
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-test
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
junit
junit
4.12
compile
mysql
mysql-connector-java
${mysql.version}
javax.servlet
servlet-api
2.5
provided
javax.servlet.jsp
jsp-api
2.0
provided
jstl
jstl
1.2
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.3.0
c3p0
c3p0
0.9.1.2
jar
compile
/**
* 实体类
*/
public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
/**
* 账户dao接口
*/
public interface AccountDao {
//保存账户信息
public void saveAccount(Account account);
//查询所有账户
public List findAll();
}
public interface AccountService {
//保存账户信息
public void saveAccount(Account account);
//查询所有账户
public List findAll();
}
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Override
public void saveAccount(Account account) {
System.out.println("业务层:保存所有账户...");
}
@Override
public List findAll() {
System.out.println("业务层:查询所有账户...");
return null;
}
}
applicationContext.xml
public class ServiceTest {
@Test
public void run1() {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
AccountService as = (AccountService) ac.getBean("accountService");
as.findAll();
}
}
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
dispatcherServlet
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
1. 编写index.jsp和list.jsp编写,超链接
2. 创建AccountController类,编写方法,进行测试
@Controller @RequestMapping("/account") public class AccountController { /** * 查询所有数据 */ @RequestMapping("/findAll") public String findAll() { System.out.println("表现层:查询所有账户..."); return "list"; } }
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
/**
* 查询所有数据
*/
@RequestMapping("/findAll")
public String findAll() {
System.out.println("表现层:查询所有账户...");
Listlist=accountService.findAll();
accountService.findAll();
return "list";
}
}
public interface AccountDao {
//保存账户信息
@Insert("insert into account (name,money) values (#{name},#{money})")
public void saveAccount(Account account);
//查询所有账户
@Select("select * from account")
public List findAll();
}
public class TestMyBatis {
/**
* 测试查询
* @throws Exception
*/
@Test
public void run1() throws Exception {
// 加载配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
// 创建SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 创建SqlSession对象
SqlSession session = factory.openSession();
// 获取到代理对象
AccountDao dao = session.getMapper(AccountDao.class);
// 查询所有数据
List list = dao.findAll();
for(Account account : list){
System.out.println(account);
}
// 关闭资源
session.close();
in.close();
}
/**
* 测试保存
* @throws Exception
*/
@Test
public void run2() throws Exception {
Account account = new Account();
account.setName("熊大");
account.setMoney(400d);
// 加载配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
// 创建SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 创建SqlSession对象
SqlSession session = factory.openSession();
// 获取到代理对象
AccountDao dao = session.getMapper(AccountDao.class);
// 保存
dao.saveAccount(account);
// 提交事务
session.commit();
// 关闭资源
session.close();
in.close();
}
}
@Repository
public interface AccountDao {
//保存账户信息
@Insert("insert into account (name,money) values (#{name},#{money})")
public void saveAccount(Account account);
//查询所有账户
@Select("select * from account")
public List findAll();
}
@Service("accountService")
public class AccountServiceImpl implements AccountService{
@Autowired
private AccountDao accountDao;
public List findAll() {
System.out.println("业务层:查询所有账户...");
return accountDao.findAll();
}
public void saveAccount(Account account) {
System.out.println("业务层:保存帐户...");
accountDao.saveAccount(account);
}
}
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
/**
* 查询所有数据
*/
@RequestMapping("/findAll")
public String findAll() {
System.out.println("表现层:查询所有账户...");
Listlist=accountService.findAll();
for (Account account:list){
System.out.println(account);
}
accountService.findAll();
return "list";
}
}
改好jsp文件
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping("/findAll")
public String findAll(Model model){
System.out.println("表现层:查询所有账户...");
// 调用service的方法
List list = accountService.findAll();
model.addAttribute("list",list);
return "list";
}
/**
* 保存
* @return
*/
@RequestMapping("/save")
public void save(Account account, HttpServletRequest request, HttpServletResponse response) throws IOException {
accountService.saveAccount(account);
response.sendRedirect(request.getContextPath()+"/account/findAll");
return;
}
}
查询:
保存: