编写springMVC框架
在web.xml中配置前端控制器servlet <!-- SpringMVC的核心控制器 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置Servlet的初始化参数,读取springmvc的配置文件,创建spring容器 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- 配置servlet启动时加载对象 --> <load-on-startup>1</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> 在resources下创建springMVC.xml,添加约束 <?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"> <!--配置spring创建容器时要扫描的包--> <context:component-scan base-package="cn"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--配置视图解析器--> <bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 设置静态资源不过滤 --> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/images/" mapping="/images/**"/> <mvc:resources location="/js/" mapping="/js/**"/> <!--配置spring开启注解mvc的支持--> <mvc:annotation-driven></mvc:annotation-driven> 编写Controller类 @Controller @RequestMapping("/account") public class AccountController { @RequestMapping("/findAll") public String findAll(){ System.out.println("表现层:查询所有的账户信息"); return "list"; } } 在WEB-INF/pages下编写成功页面整合springMVC框架
在web.xml中配置spring监听器 <!--配置spring监听器,默认只加载WEB-INF目录下的applicationContext.xml--> <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> 在Controller类中加上Service并注入,在方法中调用service的方法 @Autowired private IAccountService accountService; @RequestMapping("/findAll") public String findAll(){ System.out.println("表现层:查询所有的账户信息"); //调用service的方法 accountService.findAll(); return "list"; }编写Mybaits框架
新建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> <!--配置环境--> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/ssm"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!--引入映射配置文件--> <mappers> <!--<mapper class="cn.service.IAccountService"/>--> <package name="cn.dao"/> </mappers> </configuration> 在IAccountDao中添加注解 //查询所有账户 @Select("select * from account") public List<Account> findAll(); //保存账户信息 @Insert("insert into account values(#{name},#{money})") public void saveAccount(Account account); 测试mybits //测试查询 @Test public void run1() throws Exception { //加载配置文件 InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); //创建SqlSessionFactory对象 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //创建SqlSession对象 SqlSession sqlSession = factory.openSession(); //获取到代理对象 IAccountDao dao = sqlSession.getMapper(IAccountDao.class); //查询所有数据 List<Account> accounts = dao.findAll(); for (Account account : accounts) { System.out.println(account); } //关闭资源 sqlSession.close(); in.close(); }整合Mybatis框架
在spring配置文件applicationContext.xml文件中配置mybatis内容 <!--spring整合Mybatis--> <!--配置连接池--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql:///ssm"/> <property name="user" value="root"/> <property name="password" value="root"/> </bean> <!--配置SqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> </bean> <!--配置dao接口在的包--> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.dao"/> </bean> 删掉之前的mybaits配置文件,在dao接口类上加上@Repository注解。在Service实现类AccountServiceImpl中加上IAccountDao并注入。修改返回值 @Autowired private IAccountDao accountDao; @Override public List<Account> findAll() { System.out.println("业务层:查询所有的账户信息"); return accountDao.findAll(); } @Override public void saveAccount(Account account) { System.out.println("业务层:保存账户信息"); accountDao.saveAccount(account); } 在Controller类中调用service的方法 @Controller @RequestMapping("/account") public class AccountController { @Autowired private IAccountService accountService; @RequestMapping("/findAll") public String findAll(Model model){ System.out.println("表现层:查询所有的账户信息"); //调用service的方法 List<Account> accounts = accountService.findAll(); model.addAttribute("list",accounts); return "list"; } } 修改jsp页面上的内容 <h3>查询了所有的账户信息</h3> <c:forEach items="${list}" var="account"> ${account.name} </c:forEach>spring整合Mybatis框架配置事务
在spring配置文件applicationContext中配置spring框架声明式事务管理 <!--配置spring框架声明式事务管理--> <!--配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--配置事务通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!--名称含有find的方法只可读--> <tx:method name="find*" read-only="true"/> <!--事务级别--> <tx:method name="*" isolation="DEFAULT"/> </tx:attributes> </tx:advice> <!--配置AOP增强--> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.service.impl.*ServiceImpl.*(..))"/> </aop:config> 在index.jsp页面加上表单在Controller类上新加上方法执行保存,并转发到findAll方法 @RequestMapping("/saveAccount") public void saveAccount(Account account, HttpServletRequest request, HttpServletResponse response) throws IOException { System.out.println("表现层:保存账户信息"); //调用service的方法 accountService.saveAccount(account); response.sendRedirect(request.getContextPath()+"/account/findAll"); return ; } 完成整合