SSM框架整合
原始方式整合
- 创建数据库和表
-
创建Maven工程
-
导入maven坐标
<dependencies> <!-- 上下文--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.6</version> </dependency> <!-- AOP--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.19</version> </dependency> <!-- jdbc模板--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.6</version> </dependency> <!-- 事务--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.3.6</version> </dependency> <!-- 测试--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.6</version> </dependency> <!-- SpringMVC--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.6</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.1</version> </dependency> <!-- MyBatis相关--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!-- 分页管理插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.7.5</version> </dependency> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>0.9.1</version> </dependency> <!-- 测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> </dependency> <!-- 日志--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.5</version> </dependency> </dependencies>
-
编写实体类
public class Account { private Integer id; private String name; private Double money; public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setMoney(Double money) { this.money = money; } }
-
编写mapper接口
public interface AccountMapper { public void save(Account account); public List<Account> findAll(); }
-
创建service接口
public interface AccountService { public void save(Account account);//保存账户数据 public List<Account> findAll();//查询账户数据 }
-
创建service接口的实现类
public class AccountServiceImpl implements AccountService{ @Override public void save(Account account) { try { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = build.openSession(); AccountMapper mapper = sqlSession.getMapper(AccountMapper.class); mapper.save(account); sqlSession.commit(); sqlSession.close(); }catch (IOException e){ e.printStackTrace(); } } @Override public List<Account> findAll() { try { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = build.openSession(); AccountMapper mapper = sqlSession.getMapper(AccountMapper.class); List<Account> all = mapper.findAll(); sqlSession.close(); return all; }catch (IOException e){ e.printStackTrace(); } return null; } }
-
编写controller
@Controller @RequestMapping("/account") public class AccountController { @Autowired private AccountService accountService; //保存 @RequestMapping("/save") @ResponseBody public String save(Account account){ accountService.save(account); return "保存成功"; } // 查询 public ModelAndView findAll(){ List<Account> all = accountService.findAll(); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("accountList",all); modelAndView.setViewName("accountList"); return modelAndView; } }
-
编写页面
-
编写相关配置文件
配起来太繁琐了,根据自己的需求和之前学的老老实实配置吧
原始的方式整合弊端很多
mybatis整合spring整合
将sqlsessionFactory配置到spring容器中
一个新的配置文件sqlMapConfig-spring.xml
声明事务控制
还是上面那个配置文件:
<!-- 平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSouce"></property>
</bean>
<!-- 配置事务增强-->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 事务的aop织入-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.xxx.service.*(..))"></aop:advisor>
</aop:config>
<!-- 扫描mapper所在的包,为mapper创建实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxx.mapper"></property>
</bean>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/195238.html