java框架ssm整合_Java学习之SSM框架整合

Java学习之SSM框架整合

0x00 前言

前面的学习的Spring、Spring MVC 和Mybatis框架基本已经学习完了,但是要使用起来,我们需要把这三大框架给整合起来一起使用。

0x01 Spring 整合Spring MVC

首先要先把Spring 和Spring MVC 给整合起来,能正常运行后,再去整合Mybatis。

这里来配置一下坐标,把要使用的jar包都配置一下。

pom.xml:

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.12compile

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

编写一个实体类:

import java.io.Serializable;

public class Account implements Serializable {

private int id;

private String name;

public Account() {

}

public Account(int id, String name, double money) {

this.id = id;

this.name = name;

this.money = money;

}

private double money;

public int getId() {

return id;

}

public void setId(int 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;

}

}

dao 接口:

public interface Accountdao {

List findAll();

void saveAccount();

}

web.xml文件:

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

Archetype Created Web Application

dispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc.xml

dispatcherServlet

/

characterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

characterEncodingFilter

/*

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:applicationcontenxt.xml

applicationcontent.xml文件:

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">

这里需要配置不扫描Controller注解,Controller要交给Spring mvc来操作。

spring mvc.xml文件:

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">

这些配置完后,就可以来写其他的类了。

service接口:

public interface Accountservice {

List findAll();

void saveAccount(Account account);

}

service实现类:

@Service("accountserivce")

public class AccountserivceImpl implements Accountservice{

@Override

public List findAll() {

System.out.println("业务层,查询所有账户信息");

return null;

}

@Override

public void saveAccount(Account account) {

System.out.println("业务层,保存用户信息");

}

}

最后一步,编写controller类:

@Service("accountserivce")

//当Spring要创建AccountServiceImpl的的实例时,bean的名字必须叫做accountserivce

public class AccountserivceImpl implements Accountservice{

@Override

public List findAll() {

System.out.println("业务层,查询所有账户信息");

return null;

}

@Override

public void saveAccount(Account account) {

System.out.println("业务层,保存用户信息");

}

}

将变量注入到容器后,就可以直接来调用他的方法了。

能正常执行调用serviceimpl的方法就把这两大框架给整合完成了。

0x02 整合Mybatis

我们需要在spring配置文件里面把sqlsession工厂类给配置进去。

applicationcontenxt.xml 文件:

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">

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

配置完成后,就可以来修改dao接口了。

dao接口:

@Repository

//使用注解将该接口注入到spring

public interface Accountdao {

@Select("select * from account")

List findAll();

@Insert("insert into account (name,money)values (#{name},#{money})")

void saveAccount(Account account);

}

serviceimpl类:

@Service("accountserivce")

//当Spring要创建AccountServiceImpl的的实例时,bean的名字必须叫做accountserivce

public class AccountserivceImpl implements Accountservice{

@Autowired

// 将该变量注入到spring中

private Accountdao accountdao;

@Override

public List findAll() {

System.out.println("业务层,查询所有账户信息");

return accountdao.findAll();

}

@Override

public void saveAccount(Account account) {

System.out.println("业务层,保存用户信息");

accountdao.saveAccount(account);

}

}

controller:

@Controller

@RequestMapping("/account")

public class AccountController {

@Autowired

// 使用注解将变量注入到spring容器中

private Accountservice accountservice;

@RequestMapping("/findAll")

public String findAll(Model model){

System.out.println("表现层执行了");

List userlist = accountservice.findAll();

model.addAttribute("userlist",userlist);

return "list";

}

}

把查询内容封装到model对象里面,然后使用addAttribute方法共享到域中,在jsp页面里面写入el和jspl表达式我们就可以在页面中看到查询的内容了。

配置事务管理

在spring配置里面配置事务管理

applicationcontenxt.xml:

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

id="pt1"/>

AccountController类加多一个保存的方法,让jsp页面的表单提交到该方法后,进行获取,然后调用保存的方法进行存储数据。

@RequestMapping("/save")

public String save(Account account){

accountservice.saveAccount(account);

return "list";

}

jsp页面:

id:

姓名:

钱:

0x03 结尾

ssm的框架整合完成,这里写了一个小案例来做了整合的演示。在调试的时候频繁出问题,大部分的原因还是因为xml配置的问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过以下步骤下载Java框架SSM整合Redis: 1. 在你的项目中添加Redis相关的依赖。你可以在 Maven 或 Gradle 中添加以下依赖项: Maven: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` Gradle: ```groovy implementation 'org.springframework.boot:spring-boot-starter-data-redis' ``` 2. 在Spring的配置文件中配置Redis连接信息。你需要在`application.properties`或`application.yml`文件中添加以下Redis配置信息: ```yaml spring.redis.host=your_redis_host spring.redis.port=your_redis_port ``` 3. 创建一个Redis配置类。在你的Spring项目中,创建一个Java类来配置Redis连接池和RedisTemplate。可以参考以下示例代码: ```java @Configuration public class RedisConfig { @Value("${spring.redis.host}") private String redisHost; @Value("${spring.redis.port}") private int redisPort; @Bean public JedisConnectionFactory jedisConnectionFactory() { RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redisHost, redisPort); return new JedisConnectionFactory(redisStandaloneConfiguration); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(jedisConnectionFactory()); return template; } } ``` 4. 在需要使用Redis的类中注入`RedisTemplate`对象,并使用它来访问Redis数据库。例如,在你的Service类中注入`RedisTemplate`对象并使用它来进行缓存操作。 ```java @Service public class YourService { @Autowired private RedisTemplate<String, Object> redisTemplate; public void cacheData(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object getCachedData(String key) { return redisTemplate.opsForValue().get(key); } } ``` 以上是一个简单的示例,你可以根据你的需求进行适当的修改。 希望这对你有帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值