2019/11/19 学习笔记

2019/11/19 学习笔记

白天

Spring AOP

参考链接:https://blog.csdn.net/JinXYan/article/details/89302126

个人理解:Aop就是对OOP的补充,把我们各个业务组件中要用到的一些和业务核心功能无关的功能给抽出来,比如日志功能,基本上每个业务组件都要用到,但是却和我们的业务核心功能无关,这时候就应该把日志功能给抽出来。

可以发现运行结果和执行顺序
@Around->@Before->事件->@Around->@After->@AfterReturnings
在这里插入图片描述

Spring + Mybatis 简单整合

  • 建立一个Maven项目
  • 引入相应依赖(网上看了很多案例,使用引用的依赖有点多)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.qing</groupId>
  <artifactId>spring_mybaties2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
        <spring.version>5.2.0.RELEASE</spring.version>
        <mybatis.version>3.4.4</mybatis.version>
        <mybatisspring.version>1.3.1</mybatisspring.version>
        <jackson.version>2.7.0</jackson.version>
        <log4j.version>1.2.17</log4j.version>
        <slf4j.version>1.7.7</slf4j.version>
    </properties>
   
  <dependencies>
  <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
  </dependency>
   <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
 
  </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.2.0.RELEASE</version>
</dependency>
  
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>5.2.0.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>5.2.0.RELEASE</version>
</dependency>
	<dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-beans</artifactId>
	  <version>5.2.0.RELEASE</version>
	</dependency>

  <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-jdbc</artifactId>
	  <version>5.2.0.RELEASE</version>
	</dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>2.0.3</version>
</dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        
  </dependencies>

</project>
  • 然后建好相应层次,Dao层和entity层
    UserDao:
package com.qing.test.dao;

import java.util.List;

import com.qing.test.entity.User;

public interface UserDao {
	 public void insert(User user);

     public void delete(int userId);

     public void update(User user);

     public User findById(int userID);

     public List<User> findAll();
}

User:

package com.qing.test.entity;

import java.io.Serializable;

public class User implements Serializable{
	private int userID;
	private String userName;
	private String userPwd;
	
	public int getUserID() {
		return userID;
	}
	public void setUserID(int userID) {
		this.userID = userID;
	}
	public User() {
		super();
	}
	public User(String userName, String userPwd) {
		super();
		this.userName = userName;
		this.userPwd = userPwd;
	}
	public User(int userID, String userName, String userPwd) {
		super();
		this.userID = userID;
		this.userName = userName;
		this.userPwd = userPwd;
	}
	@Override
	public String toString() {
		return "User [userID=" + userID + ", userName=" + userName + ", userPwd=" + userPwd + "]";
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	
}
  • 然后建立Dao层对应的mybatis映射文件:
    UserDao.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="com.qing.test.dao.UserDao">
	<resultMap type="com.qing.test.entity.User" id="userResult">
              <!--其中id为主键,result为普通字段,property对应实体类属性,column对应字段名(更准确的说应该是查询结果集中的字段名)-->
              <id property="userID" column="user_id"/>
              <result property="userName" column="user_name"/>
              <result property="userPwd" column="user_pwd"/>
       </resultMap>
       
       <!-- 插入数据后获得自增主键的值 -->
       <insert id="insert" keyProperty="id" keyColumn="user_id">
              INSERT INTO user
                     (user_name,user_pwd)
              VALUES
                     (#{userName},#{userPwd})
       </insert>
       
       <!-- 修改用户内容 -->
       <update id="update">
              UPDATE user
              SET
                     user_name=#{userName},
                     user_pwd=#{userPwd},
              WHERE
                     user_id=#{userID}
       </update>
       
       <!-- 根据id删除用户 -->
       <delete id="delete">
              DELETE FROM user WHERE user_id=#{userID}
       </delete>
       
       <!-- 根据id查询用户 -->
       <select id="findById" resultMap="userResult">
              SELECT user_id,user_name,user_pwd FROM user WHERE user_id=#{userID}
       </select>
       
       <!-- 查询所有用户 -->
       <select id="findAll" resultMap="userResult">
              SELECT user_id,user_name,user_pwd FROM user
       </select>
</mapper>


  • 然后建立Mybatis和Spring配置文件(最简化 只配置了一些东西)
    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:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	
   <context:annotation-config />
	
   <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?characterEncoding=UTF-8"/>
      	<property name="username" value="root"/>
      	<property name="password" value="root"/>
   </bean>
	
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="typeAliasesPackage" value="com.qing.test.entity" />
		<property name="dataSource" ref="dataSource"/>
		<property name="mapperLocations" value="mapper/*.xml"/>
	</bean>

	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.qing.test.dao"/>
	</bean>
	
</beans>

这里的东西很少,日志也没有增加,数据库也只配置了基本的东西
最后是测试类:

package spring_mybaties2;


import java.util.List;


import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.qing.test.dao.UserDao;
import com.qing.test.entity.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test {

	@Autowired
	private UserDao userDao;
	@org.junit.Test
	public void testAdd() {
		User user = new User();
		user.setUserName("liao qing");
		user.setUserPwd("1234");
		userDao.insert(user);
	}
	@org.junit.Test
	public void testList() {
		List<User> users=userDao.findAll();
		for (User user : users) {
			System.out.println(user);
		}
	}

}

遇到的问题:
Caused by: java.lang.ClassNotFoundException: org.mybatis.spring.SqlSessionFactoryBean

需要引入mybatis-spring依赖

浪费了很多时间,要一步一步来,可以先试试mybatis有没有通,再解决spring的问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值