阿里雲大學JavaWeb開發系列課程:Spring框架入門第二十四講使用註解開發

UserAction.java

package cn.sxt.action;

import java.util.List;

import cn.sxt.service.UserService;
import cn.sxt.vo.User;

public class UserAction {
	private List<User> list;
	private UserService userService;
	
	public String list() {
		list = userService.getAll();
		return "success";
	}
	public List<User> getList(){
		return list;
	}
	public void setList(List<User> list) {
		this.list = list;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
}

UserDao.java

package cn.sxt.dao;

import java.util.List;

import cn.sxt.vo.User;

public interface UserDao {
	public List<User> getAll();
	public int add(User user);
	public int remove(int i);
}

UserDaoImpl.java

package cn.sxt.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.sxt.dao.UserDao;
import cn.sxt.vo.User;

@Repository("userDao")
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{
	@Autowired
	@Override
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		super.setSqlSessionFactory(sqlSessionFactory);
	}
	public List<User> getAll() {
		return this.getSqlSession().selectList("cn.sxt.vo.user.mapper.getAll");
	}
	public int add(User user) {
		return this.getSqlSession().insert("cn.sxt.vo.user.mapper.add",user);
	}
	public int remove(int id) {
		return this.getSqlSession().delete("cn.sxt.vo.user.mapper.remove",id);
	}
}

UserService.java

package cn.sxt.service;

import java.util.List;

import cn.sxt.vo.User;

public interface UserService {
	public List<User> getAll();
}

UserServiceImpl.java

package cn.sxt.service.impl;

import java.util.List;

import cn.sxt.dao.UserDao;
import cn.sxt.service.UserService;
import cn.sxt.vo.User;

public class UserServiceImpl implements UserService{
	private UserDao userDao;
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	@Override
	public List<User> getAll() {
		// TODO 自動生成的方法存根
		User user = new User();
		user.setName("ppp");
		user.setPwd("222");
		userDao.add(user);
		userDao.remove(40);
		return userDao.getAll();
	}
}

Test.java

package cn.sxt.test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.sxt.dao.UserDao;

public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserDao userdao = (UserDao)ac.getBean("userDao");
		List list = userDao.getAll();
		System.out.println(list.size());
	}
}

User.java

package cn.sxt.vo;

public class User {
	private int id;
	private String name;
	private	String pwd;
	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 String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
}

user.mapper.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="cn.sxt.vo.user.mapper">
	<select id="getAll" resultType="User">
		select * from user 
	</select>
	<!-- <insert id="add" parameterType="User" useGeneratedKeys="true">
		insert into user(name,pwd) values#{name},#{pwd}}
	</insert>
	<delete id="remove">
		delete from user where id=#{id}
	</delete> -->
</mapper>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 配置數據源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://localhost:3306/test"/>
		<property name="username" value="root"/>
		<property name="password" value="1012"/>
	</bean>
	<!-- 聲明式事務配置開始 -->
	<!-- 配置事務管理器 -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	<!-- 配置事務通知 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<!-- 配置哪些方法使用聲明樣的事務,配置事務的傳播特性 -->
			<tx:method name="add" propagation="REQUIRED"/>
			<tx:method name="insert" propagation="REQUIRED"/>
			<tx:method name="update" propagation="REQUIRED"/>
			<tx:method name="remove*" propagation="REQUIRED"/>
			<tx:method name="add" propagation="REQUIRED"/>
			<tx:method name="get" read-only="true"/>
			<tx:method name="*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut expression="execution(* cn.sxt.dao.impl.*.*(..))" id="pointcut"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
	</aop:config>
	<!-- 聲明式事務配置結束 -->
	<!-- 配置sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="configLocation" value="classpath:mybatis.cfg.xml"></property>
	</bean>
	<!-- 自動掃描所配置包下的所有註釋 -->
<context:component-scan base-package="cn.sxt"/>
</beans>

mybatis.cfg.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>
 	<typeAliases>
		<package name="cn.sxt.vo"/>
	</typeAliases> 
	
	<mappers>
		<mapper resource="cn/sxt/vo/user.mapper.xml"/>
	</mappers>
</configuration>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>17ssm_annotation</display-name>
  <!-- spring監聽器 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- struts2 -->
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

視頻中到這裏可以運行出結果,但我的Test有問題

這樣寫也是可以的

如果上上個圖中的@Repository不寫名字,則必須做下圖的修改

同理

 

——————————————————————————————————————————————

list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerName(); %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
		<title>My JSP 'index.jsp' starting page</title>
		<meta http-equiv="prama" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!-- 
		<link rel="stylesheet" type="text/css" href="style.css">
 		-->
</head>
<body>
	<table width="80%" align="center">
		<tr>
			<td>編號</td>
			<td>姓名</td>
			<td>密碼</td>
		</tr>
		<c:forEach items="${list }" var="bean">
		<tr>
			<td>${bean.id }</td>
			<td>${bean.name }</td>
			<td>${bean.pwd}</td>
		</tr>
		</c:forEach>
	</table>
</body>
</html>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<package name="default" extends="struts-default" namespace="/">
		<action name="list" class="userAction" method="list">
			<result>list.jsp</result>
		</action>
	</package>
</struts>

_________________________________________________________________________________________

去掉UserDaoImpl.java中的

public int add(User user) {
        return this.getSqlSession().insert("cn.sxt.vo.user.mapper.add",user);
    }
    public int remove(int id) {
        return this.getSqlSession().delete("cn.sxt.vo.user.mapper.remove",id);
    }

去掉UserDao中的

public int add(User user);
    public int remove(int i);

去掉UserServiceImpl.java中的

User user = new User();
user.setName("pppp");

userDao.add(user);
userDao.remove(40);

註解,需要在配置文件中加上一個context的命名空間,然後加一個conponent-scan,這個會去掃描。

<!-- 自動掃描所配置包下的所有註釋
     dao-@Repository("userDao")
     service-@Service("userService")
     action-Controller("userAction")
     屬性的注入:@autowired
     -->

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章