example-update
This commit is contained in:
parent
a6ecd98793
commit
c6828f5758
|
|
@ -0,0 +1,15 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<parent>
|
||||
<artifactId>Mybatis-plus</artifactId>
|
||||
<groupId>com.itcast.mp</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>MP-simple</artifactId>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package cn.itcast.mp.simple.mapper;
|
||||
|
||||
import cn.itcast.mp.simple.pojo.User;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserMapper extends BaseMapper<User> {
|
||||
|
||||
|
||||
List<User> findAll();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package cn.itcast.mp.simple.pojo;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@TableName("tb_user")
|
||||
public class User {
|
||||
|
||||
private Long id;
|
||||
private String userName;
|
||||
private String password;
|
||||
private String name;
|
||||
private Integer age;
|
||||
private String email;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<?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.itcast.mp.simple.mapper.UserMapper">
|
||||
|
||||
<!-- <resultMap id="userMap" type="cn.itcast.mp.simple.pojo.User">-->
|
||||
<!-- <result column="user_name" property="userName"></result>-->
|
||||
<!-- </resultMap>-->
|
||||
|
||||
<select id="findAll" resultType="cn.itcast.mp.simple.pojo.User">
|
||||
select * from tb_user
|
||||
</select>
|
||||
|
||||
|
||||
</mapper>
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
log4j.rootLogger=DEBUG,A1
|
||||
|
||||
log4j.appender.A1=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
<?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="development">
|
||||
<environment id="development">
|
||||
<transactionManager type="JDBC"/>
|
||||
<dataSource type="POOLED">
|
||||
<property name="driver" value="com.mysql.jdbc.Driver"/>
|
||||
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mp?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQuerie
|
||||
s=true&useSSL=false"/>
|
||||
<property name="username" value="root"/>
|
||||
<property name="password" value="root"/>
|
||||
</dataSource>
|
||||
</environment>
|
||||
</environments>
|
||||
<mappers>
|
||||
<mapper resource="UserMapper.xml"/>
|
||||
</mappers>
|
||||
</configuration>
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package cn.itcast.mp.simple;
|
||||
|
||||
import cn.itcast.mp.simple.mapper.UserMapper;
|
||||
import cn.itcast.mp.simple.pojo.User;
|
||||
import org.apache.ibatis.io.Resources;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
public class TestMybatis {
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testFindAll() throws IOException {
|
||||
|
||||
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
|
||||
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
|
||||
|
||||
SqlSession sqlSession = sqlSessionFactory.openSession();
|
||||
|
||||
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
|
||||
List<User> all = mapper.findAll();
|
||||
|
||||
for (User user : all) {
|
||||
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
sqlSession.close();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
package cn.itcast.mp.simple;
|
||||
|
||||
import cn.itcast.mp.simple.mapper.UserMapper;
|
||||
import cn.itcast.mp.simple.pojo.User;
|
||||
import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
|
||||
import org.apache.ibatis.io.Resources;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
public class TestMybatisPlus {
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testFindAll() throws IOException {
|
||||
|
||||
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
|
||||
//使用mybatisPlus提供的插件build对象构建sqlSessionFactory,就完成了mybatis及其plus之间的整合
|
||||
SqlSessionFactory sqlSessionFactory = new MybatisSqlSessionFactoryBuilder().build(resourceAsStream);
|
||||
|
||||
SqlSession sqlSession = sqlSessionFactory.openSession();
|
||||
|
||||
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
|
||||
|
||||
//使用baseMapper中的方法
|
||||
List<User> users = mapper.selectList(null);
|
||||
|
||||
for (User user : users) {
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
sqlSession.close();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<parent>
|
||||
<artifactId>Mybatis-plus</artifactId>
|
||||
<groupId>com.itcast.mp</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>MP-spring</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.version>5.1.6.RELEASE</spring.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package cn.itcast.mp.simple.mapper;
|
||||
|
||||
import cn.itcast.mp.simple.pojo.User;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
|
||||
public interface UserMapper extends BaseMapper<User> {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package cn.itcast.mp.simple.pojo;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@TableName("tb_user")
|
||||
public class User {
|
||||
|
||||
private Long id;
|
||||
private String userName;
|
||||
private String password;
|
||||
private String name;
|
||||
private Integer age;
|
||||
private String email;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<context:property-placeholder location="classpath:*.properties"/>
|
||||
|
||||
<!--定义数据源-->
|
||||
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
|
||||
<property name="driverClassName" value="${jdbc.driver}"/>
|
||||
<property name="url" value="${jdbc.url}"/>
|
||||
<property name="username" value="${jdbc.username}"/>
|
||||
<property name="password" value="${jdbc.password}"/>
|
||||
<property name="maxActive" value="10"/>
|
||||
<property name="minIdle" value="5"/>
|
||||
</bean>
|
||||
|
||||
<!--使用MP提供的sqlSessionFactory,完成mybatisPlus与spring的整合-->
|
||||
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="configLocation" value="classpath:mybatis-config.xml"/>
|
||||
<!--配置全局id自增长策略-->
|
||||
<property name="globalConfig">
|
||||
<bean class="com.baomidou.mybatisplus.core.config.GlobalConfig">
|
||||
<property name="dbConfig">
|
||||
<bean class="com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig">
|
||||
<property name="idType" value="AUTO"/>
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
</property>
|
||||
|
||||
</bean>
|
||||
<!--使用原生的Mybatis扫描器-->
|
||||
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
|
||||
<property name="basePackage" value="cn.itcast.mp.simple.mapper"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
jdbc.driver=com.mysql.jdbc.Driver
|
||||
jdbc.url=jdbc:mysql://127.0.0.1:3306/mp?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
|
||||
jdbc.username=root
|
||||
jdbc.password=root
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
log4j.rootLogger=DEBUG,A1
|
||||
|
||||
log4j.appender.A1=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
<?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="development">
|
||||
<environment id="development">
|
||||
<transactionManager type="JDBC"/>
|
||||
<dataSource type="POOLED">
|
||||
<property name="driver" value="com.mysql.jdbc.Driver"/>
|
||||
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mp?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQuerie
|
||||
s=true&useSSL=false"/>
|
||||
<property name="username" value="root"/>
|
||||
<property name="password" value="root"/>
|
||||
</dataSource>
|
||||
</environment>
|
||||
</environments>
|
||||
<mappers>
|
||||
<mapper resource="UserMapper.xml"/>
|
||||
</mappers>
|
||||
</configuration>
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package com.itcast.mp.simple;
|
||||
|
||||
import cn.itcast.mp.simple.mapper.UserMapper;
|
||||
import cn.itcast.mp.simple.pojo.User;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.junit.Test;
|
||||
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 java.util.List;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "classpath:applicationContext.xml")
|
||||
public class TestMybatisSpring {
|
||||
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Test
|
||||
public void testSelectList(){
|
||||
|
||||
List<User> users = this.userMapper.selectList(null);
|
||||
for (User user : users) {
|
||||
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<context:property-placeholder location="classpath:*.properties"/>
|
||||
|
||||
<!-- 定义数据源-->
|
||||
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
|
||||
<property name="driverClassName" value="${jdbc.driver}"/>
|
||||
<property name="url" value="${jdbc.url}"/>
|
||||
<property name="username" value="${jdbc.username}"/>
|
||||
<property name="password" value="${jdbc.password}"/>
|
||||
<property name="maxActive" value="10"/>
|
||||
<property name="minIdle" value="5"/>
|
||||
</bean>
|
||||
|
||||
<!--使用MP提供的sqlSessionFactory,完成mybatisPlus与spring的整合-->
|
||||
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
</bean>
|
||||
<!--使用原生的Mybatis扫描器-->
|
||||
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
|
||||
<property name="basePackage" value="cn.itcast.mp.simple.mapper"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
jdbc.driver=com.mysql.jdbc.Driver
|
||||
jdbc.url=jdbc:mysql://127.0.0.1:3306/mp?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
|
||||
jdbc.username=root
|
||||
jdbc.password=root
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
log4j.rootLogger=All,A1
|
||||
|
||||
log4j.appender.A1=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.itcast.mp</groupId>
|
||||
<artifactId>Mybatis-plus</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<modules>
|
||||
<module>MP-simple</module>
|
||||
<module>MP-spring</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
<!-- mybatis-plus插件依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus</artifactId>
|
||||
<version>3.1.1</version>
|
||||
</dependency>
|
||||
<!-- MySql -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.47</version>
|
||||
</dependency>
|
||||
<!-- 连接池 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>1.0.11</version>
|
||||
</dependency>
|
||||
<!--简化bean代码的工具包-->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
<version>1.18.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>1.6.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
USE db2;
|
||||
CREATE TABLE stu4(
|
||||
sid INT PRIMARY KEY AUTO_INCREMENT,
|
||||
sname VARCHAR(1000),
|
||||
age INT,
|
||||
address VARCHAR(1000),
|
||||
FOREIGN KEY(sname)REFERENCES student(createtime));
|
||||
|
||||
INSERT INTO stu1 VALUES(NULL,'张无忌',25,'光明顶'),(NULL,'赵敏',30,'光明顶1'),(NULL,'周芷若',40,'光明顶2');
|
||||
#修改张无忌的地址
|
||||
UPDATE stu1 SET address='嵩山' WHERE sname='张无忌';
|
||||
#修改周芷若的年龄和地址
|
||||
UPDATE stu1 SET address='峨眉',age=50 WHERE sname='周芷若';
|
||||
|
||||
#####mysql中的简单查询
|
||||
#查询stu表中的所有记录
|
||||
SELECT sid,sname,age,address FROM stu1;
|
||||
SELECT * FROM stu1;
|
||||
#查阅所有sid和sname,别名as
|
||||
SELECT sid AS id,sname AS `name` FROM stu1;
|
||||
#查询名字是张无忌的
|
||||
SELECT * FROM stu1 WHERE sname='张无忌';
|
||||
#查询sid大于2,且年龄大于30的
|
||||
SELECT * FROM stu1 WHERE sid>=1 AND age>30
|
||||
|
||||
|
||||
### 创建部门表
|
||||
CREATE TABLE dept (
|
||||
did INT PRIMARY KEY,
|
||||
dname VARCHAR(100)
|
||||
)
|
||||
|
||||
### 创建员工表
|
||||
CREATE TABLE emp (
|
||||
eid INT PRIMARY KEY,
|
||||
ename VARCHAR(100),
|
||||
age INT,
|
||||
edid INT,
|
||||
FOREIGN KEY(edid) REFERENCES dept(did)
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
#创建数据库
|
||||
CREATE DATABASE m224;
|
||||
|
||||
#查看数据库
|
||||
SHOW DATABASES;
|
||||
|
||||
#删除数据库
|
||||
DROP DATABASE m224;
|
||||
|
||||
#切换数据库
|
||||
USE DATABASE db1;
|
||||
|
||||
#创建数据库表
|
||||
CREATE TABLE t_stu(id INT,NAME VARCHAR(100),
|
||||
gender VARCHAR(10),brithday DATETIME,createtime TIMESTAMP);
|
||||
SHOW TABLES;
|
||||
#c查看表结构
|
||||
DESC t_stu;`student`
|
||||
DROP TABLE t_user;
|
||||
ALTER TABLE t_stu RENAME student
|
||||
|
||||
INSERT INTO student(id,NAME,gender,brithday)
|
||||
VALUES(1,'mary','女','2000-10-18');
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
# 开启事务
|
||||
START TRANSACTION;
|
||||
#第二部 执行具体事务操作
|
||||
UPDATE emp SET ename='lucya' WHERE eid =1
|
||||
# 第三部 提交/回滚事务
|
||||
ROLLBACK;
|
||||
COMMIT;
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
## 查询表有多少条记录
|
||||
SELECT COUNT(*) AS num FROM emp;
|
||||
## 查询年龄大于40人员数量
|
||||
SELECT COUNT(*) FROM emp WHERE age>40;
|
||||
# sum():求和
|
||||
SELECT SUM(age) FROM emp
|
||||
# avg(): 计算平均数
|
||||
SELECT AVG(age) FROM emp
|
||||
## 把平均数值小数点后面位数
|
||||
SELECT AVG(age) AS avgnum FROM emp
|
||||
#cast转换类型
|
||||
SELECT CAST(AVG(age) AS DECIMAL(10,2)) AS avgnum FROM emp
|
||||
#最大值和最小值
|
||||
SELECT MAX(age) FROM emp;
|
||||
SELECT MIN(age) FROM emp;
|
||||
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
# 笛卡尔积
|
||||
SELECT * FROM dept,emp
|
||||
# 内连接
|
||||
SELECT * FROM dept INNER JOIN emp ON dept.`did`=emp.`edid`
|
||||
#另一中写法
|
||||
SELECT * FROM dept,emp WHERE dept.`did`=emp.`edid`
|
||||
#左外连接
|
||||
SELECT * FROM dept LEFT JOIN emp ON dept.`did`=emp.`edid`
|
||||
# 右外连接
|
||||
SELECT * FROM dept RIGHT JOIN emp ON dept.`did`=emp.`edid`
|
||||
#A or B表独有的
|
||||
SELECT * FROM dept RIGHT JOIN emp ON dept.`did`=emp.`edid`
|
||||
WHERE dept.`did`IS NULL
|
||||
#A和B全都有
|
||||
SELECT * FROM dept LEFT JOIN emp ON dept.`did`=emp.`edid`
|
||||
UNION
|
||||
SELECT * FROM dept RIGHT JOIN emp ON dept.`did`=emp.`edid`
|
||||
|
||||
|
|
@ -0,0 +1,223 @@
|
|||
-- device_type_detail
|
||||
CREATE TABLE device_type_detail
|
||||
(
|
||||
id CHAR(20) NOT NULL COMMENT '编号',
|
||||
type_id VARCHAR(20) NOT NULL COMMENT '设备类型id 设备类型id',
|
||||
sub_group_id VARCHAR(20) COMMENT '设备分组id 设备分组id',
|
||||
NAME VARCHAR(20) NOT NULL COMMENT '设备标识 设备标识',
|
||||
show_name VARCHAR(20) NOT NULL COMMENT '型号名称 型号名称',
|
||||
specification_type VARCHAR(255) COMMENT '规格类型 规格类型',
|
||||
manufacturer VARCHAR(20) COMMENT '生产厂商 生产厂商',
|
||||
description VARCHAR(255) COMMENT '描述 描述',
|
||||
is_deleted TINYINT DEFAULT 0 NOT NULL COMMENT '是否已被删除 是否已被删除',
|
||||
create_person VARCHAR(20) NOT NULL COMMENT '创建人 创建人',
|
||||
gmt_create DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建日期 创建时间',
|
||||
gmt_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '修改时间 修改时间'
|
||||
) COMMENT '设备型号 设备型号表';
|
||||
ALTER TABLE device_type_detail
|
||||
ADD CONSTRAINT PK_device_type_detail_id PRIMARY KEY (id);
|
||||
|
||||
-- device_components_type
|
||||
CREATE TABLE device_components_type
|
||||
(
|
||||
id CHAR(20) NOT NULL COMMENT '编号',
|
||||
NAME VARCHAR(20) NOT NULL COMMENT '类型名称 设备部件类型名称',
|
||||
show_name VARCHAR(20) NOT NULL COMMENT '显示名称 显示名称',
|
||||
description VARCHAR(255) COMMENT '描述 描述',
|
||||
is_deleted TINYINT DEFAULT 0 NOT NULL COMMENT '是否已被删除 是否已被删除',
|
||||
create_person VARCHAR(20) NOT NULL COMMENT '创建人 创建人',
|
||||
gmt_create DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建日期 创建时间',
|
||||
gmt_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '修改时间 修改时间'
|
||||
) COMMENT '部件类型表 部件类型表';
|
||||
ALTER TABLE device_components_type
|
||||
ADD CONSTRAINT PK_device_ype_idEDA6 PRIMARY KEY (id);
|
||||
|
||||
-- data_model
|
||||
CREATE TABLE data_model
|
||||
(
|
||||
id CHAR(20) NOT NULL COMMENT '编号',
|
||||
type_id VARCHAR(20) NOT NULL COMMENT '设备类型id 设备类型id',
|
||||
detail_id VARCHAR(20) NOT NULL COMMENT '设备型号id 设备型号id',
|
||||
NAME VARCHAR(20) NOT NULL COMMENT '名称 名称',
|
||||
show_name VARCHAR(20) NOT NULL COMMENT '显示名称 显示名称',
|
||||
description VARCHAR(255) COMMENT '描述 描述',
|
||||
is_deleted TINYINT DEFAULT 0 NOT NULL COMMENT '是否已被删除 是否已被删除',
|
||||
create_person VARCHAR(20) NOT NULL COMMENT '创建人 创建人',
|
||||
gmt_create DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建日期 创建时间',
|
||||
gmt_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '修改时间 修改时间'
|
||||
) COMMENT '数据模型 数据模型';
|
||||
ALTER TABLE data_model
|
||||
ADD CONSTRAINT PK_data_model_id PRIMARY KEY (id);
|
||||
|
||||
-- device_type
|
||||
CREATE TABLE device_type
|
||||
(
|
||||
id CHAR(20) NOT NULL COMMENT '编号',
|
||||
NAME VARCHAR(20) NOT NULL COMMENT '设备类型名称 设备类型名称',
|
||||
show_name VARCHAR(20) NOT NULL COMMENT '显示名称 显示名称',
|
||||
description VARCHAR(255) COMMENT '描述 描述',
|
||||
is_deleted TINYINT DEFAULT 0 NOT NULL COMMENT '是否已被删除 是否已被删除',
|
||||
create_person VARCHAR(20) NOT NULL COMMENT '创建人 创建人',
|
||||
gmt_create DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建日期 创建时间',
|
||||
gmt_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '修改时间 修改时间'
|
||||
) COMMENT '设备类型 设备类型';
|
||||
ALTER TABLE device_type
|
||||
ADD CONSTRAINT PK_device_type_id PRIMARY KEY (id);
|
||||
|
||||
-- device_component_type_detail
|
||||
CREATE TABLE device_component_type_detail
|
||||
(
|
||||
id CHAR(20) NOT NULL COMMENT '编号',
|
||||
type_id VARCHAR(20) NOT NULL COMMENT '部件类型id 部件类型id',
|
||||
sub_group_id VARCHAR(20) COMMENT '部件分组id 部件分组id',
|
||||
NAME VARCHAR(20) NOT NULL COMMENT '设备标识 设备标识',
|
||||
show_name VARCHAR(20) NOT NULL COMMENT '型号名称 型号名称',
|
||||
specification_type VARCHAR(255) COMMENT '规格类型 规格类型',
|
||||
manufacturer VARCHAR(20) COMMENT '生产厂商 生产厂商',
|
||||
description VARCHAR(255) COMMENT '描述 描述',
|
||||
is_deleted TINYINT DEFAULT 0 NOT NULL COMMENT '是否已被删除 是否已被删除',
|
||||
create_person VARCHAR(20) NOT NULL COMMENT '创建人 创建人',
|
||||
gmt_create DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建日期 创建时间',
|
||||
gmt_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '修改时间 修改时间'
|
||||
) COMMENT '部件型号 部件型号表';
|
||||
ALTER TABLE device_component_type_detail
|
||||
ADD CONSTRAINT PK_device_ail_id12F4 PRIMARY KEY (id);
|
||||
|
||||
-- device_object
|
||||
CREATE TABLE device_object
|
||||
(
|
||||
id CHAR(20) NOT NULL COMMENT '编号',
|
||||
is_deleted TINYINT DEFAULT 0 NOT NULL COMMENT '是否已被删除 是否已被删除',
|
||||
create_person VARCHAR(20) NOT NULL COMMENT '创建人 创建人',
|
||||
gmt_create DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建日期 创建时间',
|
||||
gmt_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '修改时间 修改时间'
|
||||
) COMMENT '设备对象 设备对象';
|
||||
ALTER TABLE device_object
|
||||
ADD CONSTRAINT PK_device_object_id PRIMARY KEY (id);
|
||||
|
||||
-- device_area
|
||||
CREATE TABLE device_area
|
||||
(
|
||||
id CHAR(20) NOT NULL COMMENT '编号',
|
||||
NAME VARCHAR(20) NOT NULL COMMENT '区域名称 区域名称',
|
||||
description VARCHAR(255) COMMENT '描述 描述',
|
||||
object_id VARCHAR(20) NOT NULL COMMENT '设备对象id 设备对象id',
|
||||
is_deleted TINYINT DEFAULT 0 NOT NULL COMMENT '是否已被删除 是否已被删除',
|
||||
create_person VARCHAR(20) NOT NULL COMMENT '创建人 创建人',
|
||||
gmt_create DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建日期 创建时间',
|
||||
gmt_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '修改时间 修改时间'
|
||||
) COMMENT '设备区域 设备区域';
|
||||
ALTER TABLE device_area
|
||||
ADD CONSTRAINT PK_device_area_id PRIMARY KEY (id);
|
||||
|
||||
-- device_group
|
||||
/*
|
||||
警告: 字段名可能非法 - location
|
||||
*/
|
||||
CREATE TABLE device_group
|
||||
(
|
||||
id CHAR(20) NOT NULL COMMENT '编号',
|
||||
NAME VARCHAR(20) NOT NULL COMMENT '设备组标识 设备组标识',
|
||||
show_name VARCHAR(20) NOT NULL COMMENT '设备组名称 设备组名称',
|
||||
object_id VARCHAR(20) NOT NULL COMMENT '设备对象id 设备对象id',
|
||||
area_id VARCHAR(20) COMMENT '设备区域id 设备区域id',
|
||||
group_code VARCHAR(20) COMMENT '设备组编码 设备组编码',
|
||||
`LOCATION` VARCHAR(20) COMMENT '所在地 所在地',
|
||||
east_longitude VARCHAR(20) COMMENT '东经 东经',
|
||||
north_latitude VARCHAR(20) COMMENT '北纬 北纬',
|
||||
description VARCHAR(255) COMMENT '描述 描述',
|
||||
is_deleted TINYINT DEFAULT 0 NOT NULL COMMENT '是否已被删除 是否已被删除',
|
||||
create_person VARCHAR(20) NOT NULL COMMENT '创建人 创建人',
|
||||
gmt_create DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建日期 创建时间',
|
||||
gmt_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '修改时间 修改时间'
|
||||
) COMMENT '设备组 设备组';
|
||||
ALTER TABLE device_group
|
||||
ADD CONSTRAINT PK_device_group_id PRIMARY KEY (id);
|
||||
|
||||
-- device
|
||||
CREATE TABLE device
|
||||
(
|
||||
id CHAR(20) NOT NULL COMMENT '编号',
|
||||
group_id VARCHAR(20) NOT NULL COMMENT '设备组id 设备组id',
|
||||
NAME VARCHAR(20) NOT NULL COMMENT '设备标识 设备标识',
|
||||
type_id VARCHAR(20) NOT NULL COMMENT '设备类型 设备类型',
|
||||
detail_id VARCHAR(20) NOT NULL COMMENT '设备型号 设备型号',
|
||||
device_code VARCHAR(20) COMMENT '设备编码 设备编码',
|
||||
produce_time DATE NOT NULL COMMENT '出厂日期 出厂日期',
|
||||
east_longitude VARCHAR(4000) COMMENT '东经 东经',
|
||||
north_latitude VARCHAR(20) COMMENT '北纬 北纬',
|
||||
description VARCHAR(255) NOT NULL COMMENT '描述 描述',
|
||||
is_deleted TINYINT DEFAULT 0 NOT NULL COMMENT '是否已被删除 是否已被删除',
|
||||
create_person VARCHAR(20) NOT NULL COMMENT '创建人 创建人',
|
||||
gmt_create DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建日期 创建时间',
|
||||
gmt_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '修改时间 修改时间'
|
||||
) COMMENT '设备 设备';
|
||||
ALTER TABLE device
|
||||
ADD CONSTRAINT PK_device_id PRIMARY KEY (id);
|
||||
|
||||
-- device_component
|
||||
CREATE TABLE device_component
|
||||
(
|
||||
id CHAR(20) NOT NULL COMMENT '编号',
|
||||
device_id VARCHAR(20) NOT NULL COMMENT '设备id 设备id',
|
||||
sub_system_id VARCHAR(20) COMMENT '子系统id',
|
||||
type_id VARCHAR(20) NOT NULL COMMENT '部件类型id 部件设备id',
|
||||
detail_id VARCHAR(20) NOT NULL COMMENT '部件型号id 部件型号id',
|
||||
produce_company VARCHAR(20) COMMENT '生产厂商 生产厂商',
|
||||
description VARCHAR(255) COMMENT '描述 描述',
|
||||
is_deleted TINYINT DEFAULT 0 NOT NULL COMMENT '是否已被删除 是否已被删除',
|
||||
create_person VARCHAR(20) NOT NULL COMMENT '创建人 创建人',
|
||||
gmt_create DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建日期 创建时间',
|
||||
gmt_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '修改时间 修改时间'
|
||||
) COMMENT '部件 部件';
|
||||
ALTER TABLE device_component
|
||||
ADD CONSTRAINT PK_device_component_id PRIMARY KEY (id);
|
||||
|
||||
-- sub_system
|
||||
CREATE TABLE sub_system
|
||||
(
|
||||
id CHAR(20) NOT NULL COMMENT '编号',
|
||||
NAME VARCHAR(20) NOT NULL COMMENT '名称 名称',
|
||||
show_name VARCHAR(20) NOT NULL COMMENT '显示名称 显示名称',
|
||||
description VARCHAR(255) COMMENT '描述 描述',
|
||||
device_id VARCHAR(20) NOT NULL COMMENT '设备id 设备id',
|
||||
is_deleted TINYINT DEFAULT 0 NOT NULL COMMENT '是否已被删除 是否已被删除',
|
||||
create_person VARCHAR(20) NOT NULL COMMENT '创建人 创建人',
|
||||
gmt_create DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建日期 创建时间',
|
||||
gmt_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '修改时间 修改时间'
|
||||
) COMMENT '子系统 子系统';
|
||||
ALTER TABLE sub_system
|
||||
ADD CONSTRAINT PK_sub_system_id PRIMARY KEY (id);
|
||||
|
||||
-- component_sub_group
|
||||
CREATE TABLE component_sub_group
|
||||
(
|
||||
id CHAR(20) NOT NULL COMMENT '编号',
|
||||
parent_group_id VARCHAR(20) COMMENT '父组id 父组id',
|
||||
type_id VARCHAR(20) NOT NULL COMMENT '部件类型id 部件类型id',
|
||||
NAME VARCHAR(20) NOT NULL COMMENT '部件分组名称 部件分组名称',
|
||||
description VARCHAR(20) NOT NULL COMMENT '描述 描述',
|
||||
is_deleted TINYINT DEFAULT 0 NOT NULL COMMENT '是否已被删除 是否已被删除',
|
||||
create_person VARCHAR(20) NOT NULL COMMENT '创建人 创建人',
|
||||
gmt_create DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建日期 创建时间',
|
||||
gmt_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '修改时间 修改时间'
|
||||
) COMMENT '部件分组 部件分组';
|
||||
ALTER TABLE component_sub_group
|
||||
ADD CONSTRAINT PK_componeoup_id97DB PRIMARY KEY (id);
|
||||
|
||||
-- device_sub_group
|
||||
CREATE TABLE device_sub_group
|
||||
(
|
||||
id CHAR(20) NOT NULL COMMENT '编号',
|
||||
type_id VARCHAR(20) NOT NULL COMMENT '设备类型id 设备类型id',
|
||||
parent_group_id VARCHAR(20) COMMENT '父组id 父组id',
|
||||
description VARCHAR(20) NOT NULL COMMENT '描述 描述',
|
||||
NAME VARCHAR(20) NOT NULL COMMENT '部件分组名称 部件分组名称',
|
||||
is_deleted TINYINT DEFAULT 0 NOT NULL COMMENT '是否已被删除 是否已被删除',
|
||||
create_person VARCHAR(20) NOT NULL COMMENT '创建人 创建人',
|
||||
gmt_create DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建日期 创建时间',
|
||||
gmt_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '修改时间 修改时间'
|
||||
) COMMENT '设备分组 设备分组';
|
||||
ALTER TABLE device_sub_group
|
||||
ADD CONSTRAINT PK_device_sub_group_id PRIMARY KEY (id);
|
||||
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
|
||||
# 查询比“张无忌”的工资高
|
||||
## 查询张无忌工资
|
||||
## 查询工资大于张无忌工资值
|
||||
SELECT * FROM emp,salary
|
||||
WHERE emp.stid=salary.stid
|
||||
AND salary.salarystid >
|
||||
(SELECT salary.salarystid
|
||||
FROM emp,salary WHERE emp.stid=salary.stid AND emp.ename='fuck')
|
||||
|
||||
#查询和张无忌同一个部门
|
||||
SELECT emp.`ename` FROM emp,dept
|
||||
WHERE emp.`edid`=dept.`did`
|
||||
AND dept.`dname`=(SELECT dept.`dname`
|
||||
FROM emp,dept WHERE dept.`did`=emp.`edid` AND emp.`ename`='jack')
|
||||
# 统计每个部门有多少个员工()里面的是临时表
|
||||
SELECT dept.dname,es.num FROM dept,(SELECT COUNT(*) AS num,edid FROM emp GROUP BY edid) es
|
||||
WHERE dept.did=es.edid
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
#拷贝表结构
|
||||
CREATE TABLE newadmin LIKE admin;
|
||||
#拷贝表结构和数据(但约束与索引除外)
|
||||
CREATE TABLE newadmin AS ( SELECT * FROM admin ) ;
|
||||
#拷贝表结构+数据
|
||||
CREATE TABLE newadmin LIKE admin;
|
||||
INSERT INTO newadmin SELECT * FROM admin;
|
||||
#跨数据库拷贝表,shop另一个数据库
|
||||
CREATE TABLE newadmin LIKE shop.admin;
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
#去重操作,不重复显示
|
||||
SELECT DISTINCT ename FROM emp
|
||||
|
||||
#查询年龄大于20
|
||||
SELECT * FROM emp WHERE age>20
|
||||
|
||||
#模糊查询,查询所有名字开头为J的信息
|
||||
#%代表通配符,匹配任意内容
|
||||
SELECT * FROM emp WHERE ename LIKE 'j%'
|
||||
|
||||
#模糊查询,查询所有名字结尾为k的信息
|
||||
SELECT * FROM emp WHERE ename LIKE '%k'
|
||||
#模糊查询,查询所有名字中包含a的信息
|
||||
SELECT * FROM emp WHERE ename LIKE '%a%'
|
||||
|
||||
#模糊查询,查询所有名字开头为J,后面包含三个字母的信息
|
||||
#_代表站位符,匹配任意内容
|
||||
SELECT * FROM emp WHERE ename LIKE 'j___'
|
||||
|
||||
#排序操作
|
||||
##查询emp表所有记录,根据eid进行排序ASC升序,DESC降序
|
||||
SELECT * FROM emp ORDER BY eid DESC
|
||||
#查询区间范围的值
|
||||
#查询emp表年龄30-50范围
|
||||
SELECT * FROM emp WHERE age>=30 AND age<=50
|
||||
SELECT * FROM emp WHERE age BETWEEN 30 AND 50
|
||||
#查询emp表年龄是20 40 60
|
||||
SELECT * FROM emp WHERE age IN(20,40,60)
|
||||
|
||||
#分页查询
|
||||
#关键字limit,只能用在mysql中,不是标准sql
|
||||
#limit后面两个参数,第一个参数 查询数据开始位置;第二个参数 每页显示多少条记录
|
||||
SELECT * FROM emp LIMIT 0,3
|
||||
SELECT * FROM emp LIMIT 3,3
|
||||
|
||||
#分组查询GROUP BY
|
||||
#查询每个edid有多少个人
|
||||
SELECT COUNT(*) AS num,edid FROM emp GROUP BY edid
|
||||
#GROUP BY 加上having操作
|
||||
##统计每个部门的最高工资#一对多关系
|
||||
#编写emp和salary内连接查询
|
||||
##得到最高工资max
|
||||
##部门分组group by
|
||||
SELECT MAX(salarystid) ,emp.edid
|
||||
FROM emp,salary
|
||||
WHERE emp.`stid`=salary.`stid`
|
||||
GROUP BY emp.`edid`
|
||||
|
||||
# 获取最高工资大于5000的部门,having后可以加函数where后不能
|
||||
#在分组之上做筛选
|
||||
SELECT MAX(salarystand),emp.edid
|
||||
FROM emp,salary
|
||||
WHERE emp.stid=salary.stid
|
||||
GROUP BY emp.edid
|
||||
HAVING MAX(salarystand)>5000
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,224 @@
|
|||
USE db3;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS music(
|
||||
title VARCHAR(32) COMMENT '专辑名',
|
||||
alias VARCHAR(32) COMMENT '专辑别名',
|
||||
image VARCHAR(64),
|
||||
style VARCHAR(8),
|
||||
`type` VARCHAR(4),
|
||||
`medium` VARCHAR(4),
|
||||
publish_time DATE,
|
||||
publisher VARCHAR(16),
|
||||
number TINYINT,
|
||||
barcode BIGINT,
|
||||
summary VARCHAR(1024),
|
||||
artist VARCHAR(16),
|
||||
id INT PRIMARY KEY AUTO_INCREMENT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS song(
|
||||
`name` VARCHAR(32),
|
||||
`serial_number` TINYINT,
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
music_id INT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS review(
|
||||
content VARCHAR(256),
|
||||
rating TINYINT,
|
||||
review_time DATETIME,
|
||||
music_id INT,
|
||||
user_id INT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `user`(
|
||||
username VARCHAR(16) UNIQUE,
|
||||
image VARCHAR(64),
|
||||
signature VARCHAR(64),
|
||||
nickname VARCHAR(16),
|
||||
id INT PRIMARY KEY
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS user_music(
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
user_id INT,
|
||||
music_id INT
|
||||
);
|
||||
|
||||
|
||||
ALTER TABLE song ADD CONSTRAINT fn_music_song FOREIGN KEY(music_id) REFERENCES music(id);
|
||||
ALTER TABLE review ADD CONSTRAINT fn_music_review FOREIGN KEY(music_id) REFERENCES music(id);
|
||||
ALTER TABLE review ADD CONSTRAINT fn_user_review FOREIGN KEY(user_id) REFERENCES `user`(id);
|
||||
ALTER TABLE user_music ADD CONSTRAINT fn_key1 FOREIGN KEY(user_id) REFERENCES `user`(id);
|
||||
ALTER TABLE user_music ADD CONSTRAINT fn_key2 FOREIGN KEY (music_id) REFERENCES music(id);
|
||||
|
||||
|
||||
|
||||
|
||||
DROP TABLE IF EXISTS emp;
|
||||
DROP TABLE IF EXISTS dept;
|
||||
DROP TABLE IF EXISTS job;
|
||||
DROP TABLE IF EXISTS salarygrade;
|
||||
|
||||
-- 部门表
|
||||
CREATE TABLE dept (
|
||||
did INT PRIMARY KEY PRIMARY KEY, -- 部门id
|
||||
dname VARCHAR(50), -- 部门名称
|
||||
loc VARCHAR(50) -- 部门所在地
|
||||
);
|
||||
|
||||
-- 职务表,职务名称,职务描述
|
||||
CREATE TABLE job (
|
||||
id INT PRIMARY KEY,
|
||||
jname VARCHAR(20),
|
||||
description VARCHAR(50)
|
||||
);
|
||||
|
||||
-- 员工表
|
||||
CREATE TABLE emp (
|
||||
id INT PRIMARY KEY, -- 员工id
|
||||
ename VARCHAR(50), -- 员工姓名
|
||||
job_id INT, -- 职务id
|
||||
mgr INT , -- 上级领导
|
||||
joindate DATE, -- 入职日期
|
||||
salary DECIMAL(7,2), -- 工资
|
||||
bonus DECIMAL(7,2), -- 奖金
|
||||
dept_id INT -- 所在部门编号
|
||||
);
|
||||
ALTER TABLE emp ADD CONSTRAINT emp_jobid_ref_job_id_fk FOREIGN KEY (job_id) REFERENCES job (id);
|
||||
ALTER TABLE emp ADD CONSTRAINT emp_deptid_ref_dept_id_fk FOREIGN KEY (dept_id) REFERENCES dept (did);
|
||||
|
||||
|
||||
|
||||
-- 工资等级表
|
||||
CREATE TABLE salarygrade (
|
||||
grade INT PRIMARY KEY, -- 级别
|
||||
losalary INT, -- 最低工资
|
||||
hisalary INT -- 最高工资
|
||||
);
|
||||
|
||||
-- 添加4个部门
|
||||
INSERT INTO dept(did,dname,loc) VALUES
|
||||
(10,'教研部','北京'),
|
||||
(20,'学工部','上海'),
|
||||
(30,'销售部','广州'),
|
||||
(40,'财务部','深圳');
|
||||
|
||||
-- 添加4个职务
|
||||
INSERT INTO job (id, jname, description) VALUES
|
||||
(1, '董事长', '管理整个公司,接单'),
|
||||
(2, '经理', '管理部门员工'),
|
||||
(3, '销售员', '向客人推销产品'),
|
||||
(4, '文员', '使用办公软件');
|
||||
|
||||
|
||||
-- 添加员工
|
||||
INSERT INTO emp(id,ename,job_id,mgr,joindate,salary,bonus,dept_id) VALUES
|
||||
(1001,'孙悟空',4,1004,'2000-12-17','8000.00',NULL,20),
|
||||
(1002,'卢俊义',3,1006,'2001-02-20','16000.00','3000.00',30),
|
||||
(1003,'林冲',3,1006,'2001-02-22','12500.00','5000.00',30),
|
||||
(1004,'唐僧',2,1009,'2001-04-02','29750.00',NULL,20),
|
||||
(1005,'李逵',4,1006,'2001-09-28','12500.00','14000.00',30),
|
||||
(1006,'宋江',2,1009,'2001-05-01','28500.00',NULL,30),
|
||||
(1007,'刘备',2,1009,'2001-09-01','24500.00',NULL,10),
|
||||
(1008,'猪八戒',4,1004,'2007-04-19','30000.00',NULL,20),
|
||||
(1009,'罗贯中',1,NULL,'2001-11-17','50000.00',NULL,10),
|
||||
(1010,'吴用',3,1006,'2001-09-08','15000.00','0.00',30),
|
||||
(1011,'沙僧',4,1004,'2007-05-23','11000.00',NULL,20),
|
||||
(1012,'李逵',4,1006,'2001-12-03','9500.00',NULL,30),
|
||||
(1013,'小白龙',4,1004,'2001-12-03','30000.00',NULL,20),
|
||||
(1014,'关羽',4,1007,'2002-01-23','13000.00',NULL,10);
|
||||
|
||||
|
||||
-- 添加5个工资等级
|
||||
INSERT INTO salarygrade(grade,losalary,hisalary) VALUES
|
||||
(1,7000,12000),
|
||||
(2,12010,14000),
|
||||
(3,14010,20000),
|
||||
(4,20010,30000),
|
||||
(5,30010,99990);
|
||||
|
||||
|
||||
|
||||
# 查询所有员工信息。查询员工编号,员工姓名,工资,职务名称,职务描述
|
||||
SELECT emp.id,ename,salary,jname,description FROM emp LEFT JOIN job ON emp.`job_id`=job.`id` ;
|
||||
|
||||
# 查询员工编号,员工姓名,工资,职务名称,职务描述,部门名称,部门位置
|
||||
SELECT t1.id,ename,salary,jname,description,dname,loc
|
||||
FROM (SELECT emp.id id,ename,salary,jname,description,dept_id
|
||||
FROM emp LEFT JOIN job ON emp.`job_id`=job.`id`)t1
|
||||
LEFT JOIN dept ON t1. dept_id =dept.`did`;
|
||||
|
||||
# 查询员工姓名,工资,工资等级
|
||||
SELECT t2.ename,t2.salary,t1.grade
|
||||
FROM salarygrade t1 ,emp t2
|
||||
WHERE t2.salary<t1.hisalary AND t2.salary>t1.losalary;
|
||||
|
||||
#查询员工姓名,工资,职务名称,职务描述,部门名称,部门位置,工资等级
|
||||
SELECT ename,salary,jname,description,dname,loc,grade
|
||||
FROM(SELECT t1.id,ename,salary,jname,description,dname,loc
|
||||
FROM (SELECT emp.id id,ename,salary,jname,description,dept_id
|
||||
FROM emp LEFT JOIN job ON emp.`job_id`=job.`id`)t1
|
||||
LEFT JOIN dept ON t1. dept_id =dept.`did`) t2,
|
||||
salarygrade WHERE t2.salary<=salarygrade.hisalary AND t2.salary>=salarygrade.losalary;
|
||||
|
||||
|
||||
# 查询出部门编号、部门名称、部门位置、部门人数
|
||||
SELECT dept.*,t1.num '部门人数'
|
||||
FROM dept LEFT JOIN
|
||||
(SELECT dept_id,COUNT(*) num FROM emp GROUP BY dept_id) t1
|
||||
ON dept.`did`=t1.dept_id;
|
||||
|
||||
|
||||
|
||||
# 事务操作
|
||||
DROP TABLE IF EXISTS account;
|
||||
|
||||
-- 创建账户表
|
||||
CREATE TABLE account(
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
NAME VARCHAR(10),
|
||||
money DOUBLE(10,2)
|
||||
);
|
||||
|
||||
-- 添加数据
|
||||
INSERT INTO account(NAME,money) VALUES('张三',1000),('李四',1000);
|
||||
|
||||
|
||||
-- 转账操作
|
||||
-- 1.查询李四的余额
|
||||
SELECT * FROM account;
|
||||
-- 2.李四金额 -500
|
||||
UPDATE account SET money =money -500 WHERE NAME='李四';
|
||||
出错了...
|
||||
-- 3.张三金额 +500
|
||||
UPDATE account SET money =money +500 WHERE NAME ='张三';
|
||||
|
||||
|
||||
-- 带事务的转账操作
|
||||
|
||||
-- 1.查询李四的余额
|
||||
SELECT * FROM account;
|
||||
|
||||
-- 开启事务
|
||||
BEGIN;
|
||||
-- 2.李四金额 -500
|
||||
UPDATE account SET money =money -500 WHERE NAME='李四';
|
||||
出错了...
|
||||
|
||||
-- 3.张三金额 +500
|
||||
UPDATE account SET money =money +500 WHERE NAME ='张三';
|
||||
|
||||
|
||||
-- 提交数据
|
||||
COMMIT;
|
||||
|
||||
-- 回滚事务
|
||||
ROLLBACK;
|
||||
|
||||
|
||||
SELECT @@autocommit;
|
||||
# 查询到的结果是1 则表示自动提交,结果是0表示手动提交。
|
||||
# 当然也可以通过下面语句修改提交方式
|
||||
SET @@autocommit = 0;
|
||||
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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.itheima</groupId>
|
||||
<artifactId>Spring</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<modules>
|
||||
<module>spring_ioc</module>
|
||||
<module>spring_ioc_anno</module>
|
||||
<module>spring_mvc</module>
|
||||
<module>spring_jdbc</module>
|
||||
<module>spring_test</module>
|
||||
<module>spring_interceptor</module>
|
||||
<module>spring_exception_demo</module>
|
||||
<module>spring_aop</module>
|
||||
<module>spring_tx</module>
|
||||
<module>ssm</module>
|
||||
</modules>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<parent>
|
||||
<artifactId>Spring</artifactId>
|
||||
<groupId>com.itheima</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring_aop</artifactId>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>1.8.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
package com.itheima.anno;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component("myAspect")
|
||||
@Aspect //标注当前MyAspect是一个切面类
|
||||
public class MyAspect {
|
||||
|
||||
|
||||
//配置前置通知
|
||||
//@Before(value = "execution(* com.itheima.anno.*.*(..))")
|
||||
public void before(){
|
||||
System.out.println("前置增强....");
|
||||
}
|
||||
|
||||
public void afterReturning(){
|
||||
System.out.println("后置增强....");
|
||||
}
|
||||
|
||||
//Proceeding JoinPoint:正在执行的连接点->切点
|
||||
@Around(value = "pointcut()")
|
||||
public Object around(ProceedingJoinPoint pjp) throws Throwable {
|
||||
System.out.println("环绕前....");
|
||||
Object proceed = pjp.proceed();//切点方法
|
||||
System.out.println("环绕后...");
|
||||
return proceed;
|
||||
}
|
||||
|
||||
public void afterThrowing(){
|
||||
System.out.println("异常抛出后增强....");
|
||||
}
|
||||
@After(value = "MyAspect.pointcut()")
|
||||
public void after(){
|
||||
System.out.println("最终增强....");
|
||||
}
|
||||
|
||||
//定义节点表达式
|
||||
@Pointcut("execution(* com.itheima.anno.*.*(..))")
|
||||
public void pointcut(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package com.itheima.anno;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component("target")
|
||||
public class Target implements TargetInterface {
|
||||
public void save() {
|
||||
System.out.println("save running....");
|
||||
// int i=1/0;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package com.itheima.anno;
|
||||
|
||||
public interface TargetInterface {
|
||||
|
||||
|
||||
public void save();
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package com.itheima.aop;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
|
||||
public class MyAspect {
|
||||
|
||||
|
||||
public void before(){
|
||||
System.out.println("前置增强....");
|
||||
}
|
||||
|
||||
public void afterReturning(){
|
||||
System.out.println("后置增强....");
|
||||
}
|
||||
|
||||
//Proceeding JoinPoint:正在执行的连接点->切点
|
||||
public Object around(ProceedingJoinPoint pjp) throws Throwable {
|
||||
System.out.println("环绕前....");
|
||||
Object proceed = pjp.proceed();//切点方法
|
||||
System.out.println("环绕后...");
|
||||
return proceed;
|
||||
}
|
||||
|
||||
public void afterThrowing(){
|
||||
System.out.println("异常抛出后增强....");
|
||||
}
|
||||
|
||||
public void after(){
|
||||
System.out.println("最终增强....");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package com.itheima.aop;
|
||||
|
||||
public class Target implements TargetInterface {
|
||||
public void save() {
|
||||
System.out.println("save running....");
|
||||
// int i=1/0;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package com.itheima.aop;
|
||||
|
||||
public interface TargetInterface {
|
||||
|
||||
|
||||
public void save();
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package com.itheima.proxy.cglib;
|
||||
|
||||
public class Advice {
|
||||
|
||||
|
||||
public void before(){
|
||||
System.out.println("前置增强...");
|
||||
}
|
||||
|
||||
public void afterReturning(){
|
||||
System.out.println("后置增强...");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
package com.itheima.proxy.cglib;
|
||||
|
||||
import com.itheima.proxy.jdk.TargetInterface;
|
||||
import org.springframework.cglib.proxy.Enhancer;
|
||||
import org.springframework.cglib.proxy.MethodInterceptor;
|
||||
import org.springframework.cglib.proxy.MethodProxy;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
public class ProxyTest {
|
||||
|
||||
|
||||
/**
|
||||
* 基于cglib的动态生成代理对象的方法
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
//创建目标对象
|
||||
final Target target=new Target();
|
||||
|
||||
//增强对象
|
||||
final Advice advice=new Advice();
|
||||
|
||||
//返回值就是动态生成的代理对象
|
||||
//1.创建增强器
|
||||
Enhancer enhancer=new Enhancer();
|
||||
//2.设置父类(目标)
|
||||
enhancer.setSuperclass(Target.class);
|
||||
//3.设置回调函数
|
||||
enhancer.setCallback(new MethodInterceptor() {
|
||||
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
|
||||
advice.before();//执行前置
|
||||
Object invoke = method.invoke(target, args);//执行目标
|
||||
advice.afterReturning();//执行后置
|
||||
return invoke;
|
||||
}
|
||||
});
|
||||
//4.创建代理对象
|
||||
Target proxy = (Target) enhancer.create();
|
||||
proxy.save();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package com.itheima.proxy.cglib;
|
||||
|
||||
|
||||
|
||||
public class Target {
|
||||
public void save() {
|
||||
System.out.println("save running.....");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package com.itheima.proxy.jdk;
|
||||
|
||||
public class Advice {
|
||||
|
||||
|
||||
public void before(){
|
||||
System.out.println("前置增强...");
|
||||
}
|
||||
|
||||
public void afterReturning(){
|
||||
System.out.println("后置增强...");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
package com.itheima.proxy.jdk;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
public class ProxyTest {
|
||||
|
||||
|
||||
/**
|
||||
* 基于JDK的动态生成代理对象的方法
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
//创建目标对象
|
||||
final Target target=new Target();
|
||||
|
||||
//增强对象
|
||||
final Advice advice=new Advice();
|
||||
|
||||
//返回值就是动态生成的代理对象
|
||||
TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(
|
||||
target.getClass().getClassLoader(),//目标对象的类加载器
|
||||
target.getClass().getInterfaces(),//目标对象相同的接口字节码对象数组
|
||||
new InvocationHandler() {
|
||||
//调用代理对象的任何方法,是指执行的都是invoke方法
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
advice.before();//前置增强
|
||||
Object invoke = method.invoke(target, args);//执行目标方法
|
||||
advice.afterReturning();//后置增强
|
||||
return invoke;
|
||||
}
|
||||
}
|
||||
);
|
||||
//调用代理对象的方法
|
||||
proxy.save();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package com.itheima.proxy.jdk;
|
||||
|
||||
public class Target implements TargetInterface {
|
||||
public void save() {
|
||||
System.out.println("save running.....");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package com.itheima.proxy.jdk;
|
||||
|
||||
public interface TargetInterface {
|
||||
|
||||
|
||||
public void save();
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
|
||||
<!--开启组件扫描-->
|
||||
<context:component-scan base-package="com.itheima.anno"></context:component-scan>
|
||||
|
||||
<!--aop自动代理-->
|
||||
<aop:aspectj-autoproxy/>
|
||||
|
||||
|
||||
</beans>
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
<?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"
|
||||
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">
|
||||
|
||||
<!--将切面和目标对象都交给spring容器去管-->
|
||||
<!--目标对象-->
|
||||
<bean id="target" class="com.itheima.aop.Target"/>
|
||||
|
||||
<!--切面对象-->
|
||||
<bean id="myAspect" class="com.itheima.aop.MyAspect"/>
|
||||
|
||||
<!--告诉spring,哪些方法需要被进行哪些增强(前置、后置...),即配置织入,先配置aop命名空间和约束位置,在最上面-->
|
||||
<aop:config>
|
||||
<!--声明切面-->
|
||||
<aop:aspect ref="myAspect">
|
||||
|
||||
<!--配置抽取切点表达式-->
|
||||
<aop:pointcut id="myPoint" expression="execution(* com.itheima.aop.*.*(..))"/>
|
||||
|
||||
|
||||
<!--切面:切点+通知-->
|
||||
<!-- <aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.save())"></aop:before>-->
|
||||
<!-- com.itheima.aop包下的任一类返回值也任意的任意方法都可以被增强-->
|
||||
<!-- <aop:before method="before" pointcut="execution(* com.itheima.aop.*.*(..))"></aop:before>-->
|
||||
<!-- <!–后置–>-->
|
||||
<!-- <aop:after-returning method="afterReturning"-->
|
||||
<!-- pointcut="execution(* com.itheima.aop.*.*(..))"></aop:after-returning>-->
|
||||
<!--环绕-->
|
||||
<aop:around method="around" pointcut="execution(* com.itheima.aop.*.*(..))"/>
|
||||
<aop:around method="around" pointcut-ref="myPoint"/>
|
||||
<!--异常抛出后执行-->
|
||||
<!-- <aop:after-throwing method="afterThrowing" pointcut="execution(* com.itheima.aop.*.*(..))"/>-->
|
||||
<!--最终执行-->
|
||||
<!-- <aop:after method="after" pointcut="execution(* com.itheima.aop.*.*(..))"/>-->
|
||||
|
||||
</aop:aspect>
|
||||
</aop:config>
|
||||
|
||||
|
||||
</beans>
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
jdbc.driver=com.mysql.jdbc.Driver
|
||||
jdbc.url=jdbc:mysql:///spring_jdbc
|
||||
jdbc.username=root
|
||||
jdbc.password=root
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
### direct log messages to stdout ###
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.Target=System.out
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
|
||||
|
||||
### direct messages to file mylog.log ###
|
||||
log4j.appender.file=org.apache.log4j.FileAppender
|
||||
log4j.appender.file.File=c:/mylog.log
|
||||
log4j.appender.file.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
|
||||
|
||||
### set log levels - for more verbose logging change 'info' to 'debug' ###
|
||||
|
||||
log4j.rootLogger=info, stdout
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
<?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:mvc="http://www.springframework.org/schema/mvc"
|
||||
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/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">
|
||||
|
||||
<!-- 1.mvc注解驱动-->
|
||||
<mvc:annotation-driven/>
|
||||
|
||||
<!-- 2.配置视图解析器-->
|
||||
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
<property name="prefix" value="/pages/"/>
|
||||
<property name="suffix" value=".jsp"/>
|
||||
</bean>
|
||||
|
||||
<!-- 3.静态资源权限开放-->
|
||||
<mvc:default-servlet-handler/>
|
||||
|
||||
<!-- 组件扫描,扫描controller-->
|
||||
<context:component-scan base-package="com.itheima.controller"/>
|
||||
|
||||
<!-- 配置权限拦截器-->
|
||||
<mvc:interceptors>
|
||||
<mvc:interceptor>
|
||||
<!--配置对哪些资源执行拦截操作-->
|
||||
<mvc:mapping path="/**"/>
|
||||
<!--配置哪些资源排除拦截操作-->
|
||||
<mvc:exclude-mapping path="/user/login"/>
|
||||
<bean class="com.itheima.interceptor.PrivilegeInterceptor"/>
|
||||
</mvc:interceptor>
|
||||
</mvc:interceptors>
|
||||
|
||||
|
||||
</beans>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
|
||||
version="4.0">
|
||||
</web-app>
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package com.itheima.test;
|
||||
|
||||
import com.itheima.anno.TargetInterface;
|
||||
import org.junit.Test;
|
||||
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;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:applicationContext-anno.xml")
|
||||
public class AnnoTest {
|
||||
|
||||
|
||||
@Autowired
|
||||
private TargetInterface target;
|
||||
|
||||
|
||||
@Test
|
||||
public void test1(){
|
||||
target.save();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package com.itheima.test;
|
||||
|
||||
import com.itheima.aop.TargetInterface;
|
||||
import com.itheima.proxy.cglib.Target;
|
||||
import org.junit.Test;
|
||||
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;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:applicationContext.xml")
|
||||
public class AopTest {
|
||||
|
||||
|
||||
@Autowired
|
||||
private TargetInterface target;
|
||||
|
||||
@Test
|
||||
public void test1(){
|
||||
target.save();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,151 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<parent>
|
||||
<artifactId>Spring</artifactId>
|
||||
<groupId>com.itheima</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring_exception_demo</artifactId>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.32</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>c3p0</groupId>
|
||||
<artifactId>c3p0</artifactId>
|
||||
<version>0.9.1.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>1.1.10</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp</groupId>
|
||||
<artifactId>javax.servlet.jsp-api</artifactId>
|
||||
<version>2.2.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>2.9.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.9.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
<version>2.9.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-fileupload</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
<version>1.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>1.7.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jstl</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!--tomcat插件-->
|
||||
<plugin>
|
||||
<groupId>org.apache.tomcat.maven</groupId>
|
||||
<artifactId>tomcat7-maven-plugin</artifactId>
|
||||
<version>2.2</version>
|
||||
<configuration>
|
||||
<port>8080</port>
|
||||
<!-- <path>/</path>-->
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>7</source>
|
||||
<target>7</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package com.itheima.controller;
|
||||
|
||||
|
||||
import com.itheima.exception.MyException;
|
||||
import com.itheima.service.DemoService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
@Controller
|
||||
public class DemoController {
|
||||
|
||||
@Autowired
|
||||
private DemoService demoService;
|
||||
|
||||
@RequestMapping(value = "/show")
|
||||
public String show() throws FileNotFoundException, MyException {
|
||||
System.out.println("show running......");
|
||||
demoService.show1();
|
||||
//demoService.show2();
|
||||
//demoService.show3();
|
||||
//demoService.show4();
|
||||
//demoService.show5();
|
||||
return "index";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package com.itheima.exception;
|
||||
|
||||
public class MyException extends Exception {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
package com.itheima.resovler;
|
||||
|
||||
import com.itheima.exception.MyException;
|
||||
import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
public class MyExceptionResolver implements HandlerExceptionResolver {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param httpServletRequest
|
||||
* @param httpServletResponse
|
||||
* @param o
|
||||
* @param e :异常对象
|
||||
* @return ModelAndView:跳转到错误视图信息
|
||||
*/
|
||||
@Override
|
||||
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
|
||||
|
||||
ModelAndView modelAndView = new ModelAndView();
|
||||
//可以对异常处理进行判断
|
||||
if(e instanceof MyException){
|
||||
modelAndView.addObject("info","自定义异常");
|
||||
}else if(e instanceof ClassCastException){
|
||||
modelAndView.addObject("info","类转换异常");
|
||||
|
||||
}
|
||||
modelAndView.setViewName("error");
|
||||
|
||||
return modelAndView;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package com.itheima.service;
|
||||
|
||||
|
||||
|
||||
import com.itheima.exception.MyException;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
public interface DemoService {
|
||||
void show1();
|
||||
|
||||
void show2();
|
||||
|
||||
void show3() throws FileNotFoundException;
|
||||
|
||||
void show4();
|
||||
|
||||
void show5() throws MyException;
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package com.itheima.service;
|
||||
|
||||
import com.itheima.exception.MyException;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class DemoServiceImpl implements DemoService {
|
||||
public void show1() {
|
||||
System.out.println("抛出类型转换异常....");
|
||||
Object str = "zhangsan";
|
||||
Integer num = (Integer)str;
|
||||
}
|
||||
|
||||
public void show2() {
|
||||
System.out.println("抛出除零异常....");
|
||||
int i = 1/0;
|
||||
}
|
||||
|
||||
public void show3() throws FileNotFoundException {
|
||||
System.out.println("文件找不到异常....");
|
||||
InputStream in = new FileInputStream("C:/xxx/xxx/xxx.txt");
|
||||
}
|
||||
|
||||
public void show4() {
|
||||
System.out.println("空指针异常.....");
|
||||
String str = null;
|
||||
str.length();
|
||||
}
|
||||
|
||||
public void show5() throws MyException {
|
||||
System.out.println("自定义异常....");
|
||||
throw new MyException();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd
|
||||
">
|
||||
|
||||
<bean id="demoService" class="com.itheima.service.DemoServiceImpl"></bean>
|
||||
|
||||
|
||||
</beans>
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
### direct log messages to stdout ###
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.Target=System.out
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
|
||||
|
||||
### direct messages to file mylog.log ###
|
||||
log4j.appender.file=org.apache.log4j.FileAppender
|
||||
log4j.appender.file.File=c:/mylog.log
|
||||
log4j.appender.file.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
|
||||
|
||||
### set log levels - for more verbose logging change 'info' to 'debug' ###
|
||||
|
||||
log4j.rootLogger=info, stdout
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
<?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:mvc="http://www.springframework.org/schema/mvc"
|
||||
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/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
|
||||
">
|
||||
|
||||
<!--1、mvc注解驱动-->
|
||||
<mvc:annotation-driven/>
|
||||
|
||||
<!--2、配置视图解析器-->
|
||||
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
<property name="prefix" value="/"/>
|
||||
<property name="suffix" value=".jsp"/>
|
||||
</bean>
|
||||
|
||||
<!--3、静态资源权限开放-->
|
||||
<mvc:default-servlet-handler/>
|
||||
|
||||
<!--4、组件扫描 扫描Controller-->
|
||||
<context:component-scan base-package="com.itheima.controller"/>
|
||||
|
||||
<!--配置异常处理器-->
|
||||
<!-- <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">-->
|
||||
<!-- <property name="defaultErrorView" value="error"/>-->
|
||||
<!-- <property name="exceptionMappings">-->
|
||||
<!-- <map>-->
|
||||
<!-- <entry key="java.lang.ClassCastException" value="error1"/>-->
|
||||
<!-- <entry key="com.itheima.exception.MyException" value="error2"/>-->
|
||||
<!-- </map>-->
|
||||
<!-- </property>-->
|
||||
<!-- </bean>-->
|
||||
|
||||
|
||||
<!--自定义异常处理器-->
|
||||
<bean class="com.itheima.resovler.MyExceptionResolver"/>
|
||||
|
||||
|
||||
</beans>
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
|
||||
version="4.0">
|
||||
|
||||
<!-- 解决乱码的过滤器-->
|
||||
<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>
|
||||
|
||||
|
||||
<!-- 全局的初始化参数-->
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>classpath:applicationContext.xml</param-value>
|
||||
</context-param>
|
||||
|
||||
|
||||
<!-- 配置Spring监听器-->
|
||||
<listener>
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<!-- springMVC前端控制器-->
|
||||
<servlet>
|
||||
<servlet-name>DispatcherServlet</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<init-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>classpath:spring-mvc.xml</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>2</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>DispatcherServlet</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
|
||||
</web-app>
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>通用的错误提示页面</h1>
|
||||
<h1>${info}</h1>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>类型转换异常</h1>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>自定义异常</h1>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
<html>
|
||||
<body>
|
||||
<h2>Hello World!</h2>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,153 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<parent>
|
||||
<artifactId>Spring</artifactId>
|
||||
<groupId>com.itheima</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring_interceptor</artifactId>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.32</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>c3p0</groupId>
|
||||
<artifactId>c3p0</artifactId>
|
||||
<version>0.9.1.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>1.1.10</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp</groupId>
|
||||
<artifactId>javax.servlet.jsp-api</artifactId>
|
||||
<version>2.2.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>2.9.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.9.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
<version>2.9.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-fileupload</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
<version>1.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>1.7.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jstl</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!--tomcat插件-->
|
||||
<plugin>
|
||||
<groupId>org.apache.tomcat.maven</groupId>
|
||||
<artifactId>tomcat7-maven-plugin</artifactId>
|
||||
<version>2.2</version>
|
||||
<configuration>
|
||||
<port>8080</port>
|
||||
<!-- <path>/</path>-->
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>7</source>
|
||||
<target>7</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package com.itheima.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
@Controller
|
||||
public class TargetController {
|
||||
|
||||
@RequestMapping("/target")
|
||||
public ModelAndView show(ModelAndView modelAndView){
|
||||
System.out.println("目标资源执行...");
|
||||
modelAndView.addObject("name","itcast");
|
||||
modelAndView.setViewName("index.jsp");
|
||||
return modelAndView;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package com.itheima.interceptor;
|
||||
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
public class MyInterceptor1 implements HandlerInterceptor {
|
||||
|
||||
//在目标方法执行之前 执行
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
System.out.println("preHandler...");
|
||||
String param = request.getParameter("param");
|
||||
if("yes".equals(param)){
|
||||
return true;
|
||||
}else {
|
||||
//如果返回true代表放行 返回false代表不放行
|
||||
request.getRequestDispatcher("/error.jsp").forward(request,response);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//目标方法执行之后 视图对象返回之前执行
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
||||
modelAndView.addObject("name","itheima");
|
||||
System.out.println("postHandler...");
|
||||
}
|
||||
|
||||
//在整个流程都执行完毕之后 执行
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
System.out.println("afterCompletion...");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package com.itheima.interceptor;
|
||||
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
public class MyInterceptor2 implements HandlerInterceptor {
|
||||
|
||||
//在目标方法执行之前 执行
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
System.out.println("preHandler222...");
|
||||
return true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
//目标方法执行之后 视图对象返回之前执行
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
||||
|
||||
System.out.println("postHandler222...");
|
||||
}
|
||||
|
||||
//在整个流程都执行完毕之后 执行
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
System.out.println("afterCompletion222...");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
jdbc.driver=com.mysql.jdbc.Driver
|
||||
jdbc.url=jdbc:mysql:///spring_jdbc
|
||||
jdbc.username=root
|
||||
jdbc.password=root
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
<?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:mvc="http://www.springframework.org/schema/mvc"
|
||||
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/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">
|
||||
|
||||
<!-- 1.mvc注解驱动-->
|
||||
<mvc:annotation-driven/>
|
||||
|
||||
<!-- <!– 2.配置视图解析器–>-->
|
||||
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
|
||||
<!-- <property name="prefix" value="/pages/"/>-->
|
||||
<!-- <property name="suffix" value=".jsp"/>-->
|
||||
<!-- </bean>-->
|
||||
|
||||
<!-- 3.静态资源权限开放-->
|
||||
<mvc:default-servlet-handler/>
|
||||
|
||||
<!-- 组件扫描,扫描controller-->
|
||||
<context:component-scan base-package="com.itheima.controller"/>
|
||||
|
||||
<!-- 配置拦截器-->
|
||||
<mvc:interceptors>
|
||||
<mvc:interceptor>
|
||||
<!--代表对哪些资源执行拦截操作-->
|
||||
<mvc:mapping path="/**"/>
|
||||
<bean class="com.itheima.interceptor.MyInterceptor1"/>
|
||||
</mvc:interceptor>
|
||||
|
||||
<mvc:interceptor>
|
||||
<!--代表对哪些资源执行拦截操作-->
|
||||
<mvc:mapping path="/**"/>
|
||||
<bean class="com.itheima.interceptor.MyInterceptor2"/>
|
||||
</mvc:interceptor>
|
||||
|
||||
</mvc:interceptors>
|
||||
|
||||
|
||||
</beans>
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
|
||||
version="4.0">
|
||||
|
||||
|
||||
<!-- <!– 全局的初始化参数–>-->
|
||||
<!-- <context-param>-->
|
||||
<!-- <param-name>contextConfigLocation</param-name>-->
|
||||
<!-- <param-value>classpath:applicationContext.xml</param-value>-->
|
||||
<!-- </context-param>-->
|
||||
|
||||
|
||||
<!-- 配置Spring监听器-->
|
||||
<!-- <listener>-->
|
||||
<!-- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-->
|
||||
<!-- </listener>-->
|
||||
|
||||
<!-- springMVC前端控制器-->
|
||||
<servlet>
|
||||
<servlet-name>DispatcherServlet</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<init-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>classpath:spring-mvc.xml</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>2</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>DispatcherServlet</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
</web-app>
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
<%--
|
||||
Created by IntelliJ IDEA.
|
||||
User: Administrator
|
||||
Date: 2022/4/12
|
||||
Time: 16:23
|
||||
To change this template use File | Settings | File Templates.
|
||||
--%>
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>error</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>error</h1>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
||||
pageEncoding="UTF-8"%>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
|
||||
"http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>主页</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h2>Hello World! ${name}</h2>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<parent>
|
||||
<artifactId>Spring</artifactId>
|
||||
<groupId>com.itheima</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring_ioc</artifactId>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- spring框架-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<!-- junit单元测试包-->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<scope>com.itheima.test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package com.itheima.dao;
|
||||
|
||||
public interface UserDao {
|
||||
|
||||
public void save();
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
package com.itheima.dao.impl;
|
||||
|
||||
import com.itheima.dao.UserDao;
|
||||
import com.itheima.domain.User;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
public class UserDaoImpl implements UserDao {
|
||||
|
||||
private List<String> strList;
|
||||
private Map<String, User> userMap;
|
||||
private Properties properties;
|
||||
|
||||
|
||||
public void setStrList(List<String> strList) {
|
||||
this.strList = strList;
|
||||
}
|
||||
|
||||
public void setUserMap(Map<String, User> userMap) {
|
||||
this.userMap = userMap;
|
||||
}
|
||||
|
||||
public void setProperties(Properties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
private String username;
|
||||
private int age;
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
//复写无参构造,测试scope属性的创建时机
|
||||
// public UserDaoImpl() {
|
||||
//
|
||||
// System.out.println("UserDaoImpl创建...");
|
||||
// }
|
||||
|
||||
// public void init(){
|
||||
// System.out.println("初始化方法...");
|
||||
// }
|
||||
//
|
||||
// public void destroy(){
|
||||
// System.out.println("销毁方法...");
|
||||
// }
|
||||
|
||||
public void save(){
|
||||
|
||||
// System.out.println(username+"======"+age);
|
||||
|
||||
System.out.println(strList);
|
||||
System.out.println(userMap);
|
||||
System.out.println(properties);
|
||||
|
||||
System.out.println("save running。。。");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
package com.itheima.demo;
|
||||
|
||||
import com.itheima.service.UserService;
|
||||
import com.itheima.service.impl.UserServiceImpl;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.context.support.FileSystemXmlApplicationContext;
|
||||
|
||||
public class UserController {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
//使用类加载路径进行加载文件根目录下的xml文件,resource目录下的就是
|
||||
//ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||
//从磁盘路径进行加载
|
||||
ApplicationContext app=new FileSystemXmlApplicationContext("E:\\self_example\\Spring\\spring_ioc\\src\\main\\resources\\applicationContext.xml");
|
||||
|
||||
//根据id获取,允许多个类相同id不同的类加载路径
|
||||
//UserService userService = (UserService) app.getBean("userService");
|
||||
//不允许出现多个类型相同的
|
||||
UserService userService = app.getBean(UserService.class);
|
||||
|
||||
//只有从spring的容器方法才能获取到依赖注入
|
||||
userService.save();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package com.itheima.demo;
|
||||
|
||||
import com.itheima.dao.UserDao;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class UserDaoDemo {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//获取spring容器中的context
|
||||
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||
|
||||
UserDao userDao = (UserDao) app.getBean("userDao");
|
||||
userDao.save();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package com.itheima.domain;
|
||||
|
||||
import javax.print.DocFlavor;
|
||||
|
||||
public class User {
|
||||
|
||||
private String name;
|
||||
private String addr;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddr() {
|
||||
return addr;
|
||||
}
|
||||
|
||||
public void setAddr(String addr) {
|
||||
this.addr = addr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"name='" + name + '\'' +
|
||||
", addr='" + addr + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package com.itheima.factory;
|
||||
|
||||
import com.itheima.dao.UserDao;
|
||||
import com.itheima.dao.impl.UserDaoImpl;
|
||||
|
||||
public class DynamicFactory {
|
||||
|
||||
|
||||
//由于不是静态的,所以需要先创建DynamicFactory对象,在调用该方法
|
||||
//使用动态实例方法返回dao层对象
|
||||
public UserDao getUserDao(){
|
||||
return new UserDaoImpl();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package com.itheima.factory;
|
||||
|
||||
import com.itheima.dao.UserDao;
|
||||
import com.itheima.dao.impl.UserDaoImpl;
|
||||
|
||||
public class StaticFactory {
|
||||
|
||||
//使用静态方法返回dao层对象
|
||||
public static UserDao getUserDao(){
|
||||
return new UserDaoImpl();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package com.itheima.service;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
public void save();
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package com.itheima.service.impl;
|
||||
|
||||
import com.itheima.dao.UserDao;
|
||||
import com.itheima.service.UserService;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
|
||||
private UserDao userDao;
|
||||
|
||||
|
||||
// //使用set方法进行依赖注入
|
||||
// public void setUserDao(UserDao userDao){
|
||||
// this.userDao=userDao;
|
||||
// }
|
||||
|
||||
|
||||
public UserServiceImpl() {
|
||||
}
|
||||
|
||||
//使用有参构造完成依赖注入
|
||||
public UserServiceImpl(UserDao userDao) {
|
||||
this.userDao = userDao;
|
||||
}
|
||||
|
||||
public void save() {
|
||||
// ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||
//
|
||||
// UserDao userDao = (UserDao) app.getBean("userDao");
|
||||
userDao.save();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
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">
|
||||
|
||||
</beans>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
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">
|
||||
|
||||
</beans>
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
<?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:p="http://www.springframework.org/schema/p"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<!-- <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" scope="singleton" ></bean>-->
|
||||
|
||||
<!-- <bean id="userDao" class="com.itheima.factory.StaticFactory" factory-method="getUserDao" scope="singleton" ></bean>-->
|
||||
|
||||
<!-- <bean id="factory" class="com.itheima.factory.DynamicFactory"></bean>-->
|
||||
<!-- <bean id="userDao" factory-bean="factory" factory-method="getUserDao"></bean>-->
|
||||
|
||||
<!-- <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">-->
|
||||
<!--<!– set方法普通数据类型的一依赖注入–>-->
|
||||
<!-- <property name="username" value="zhangsan"></property>-->
|
||||
<!-- <property name="age" value="18"></property>-->
|
||||
<!-- </bean> -->
|
||||
<bean id="user1" class="com.itheima.domain.User">
|
||||
<property name="name" value="tom"></property>
|
||||
<property name="addr" value="beijing"></property>
|
||||
</bean>
|
||||
|
||||
<bean id="user2" class="com.itheima.domain.User">
|
||||
<property name="name" value="luck"/>
|
||||
<property name="addr" value="tianjing"/>
|
||||
</bean>
|
||||
|
||||
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
|
||||
<!-- set方法集合数据类型的一依赖注入-->
|
||||
<property name="strList">
|
||||
<list>
|
||||
<value>aaa</value>
|
||||
<value>bbb</value>
|
||||
<value>ccc</value>
|
||||
</list>
|
||||
</property>
|
||||
|
||||
<property name="userMap">
|
||||
<map>
|
||||
<entry key="u1" value-ref="user1"></entry>
|
||||
<entry key="u2" value-ref="user2"></entry>
|
||||
</map>
|
||||
</property>
|
||||
|
||||
<property name="properties">
|
||||
<props>
|
||||
<prop key="p1">ppp1</prop>
|
||||
<prop key="p2">ppp2</prop>
|
||||
<prop key="p3">ppp3</prop>
|
||||
</props>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- <bean id="userService" class="com.itheima.service.impl.UserServiceImpl">-->
|
||||
<!-- <!–依赖注入–>-->
|
||||
<!-- <property name="userDao" ref="userDao"></property>-->
|
||||
<!-- </bean>-->
|
||||
|
||||
<!-- 使用p命名空间,简化依赖注入-->
|
||||
<!-- <bean id="userService" class="com.itheima.service.impl.UserServiceImpl" p:userDao-ref="userDao"></bean>-->
|
||||
|
||||
<!--使用有参构造的方式完成依赖注入-->
|
||||
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
|
||||
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
|
||||
</bean>
|
||||
|
||||
<!--分模块开发,引入其他的applicationContext-->
|
||||
<!--只用加载主文件,那么分文件就会被加载-->
|
||||
<import resource="applicationContext-user.xml"></import>
|
||||
<import resource="applicationContext-product.xml"></import>
|
||||
|
||||
|
||||
</beans>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
|
||||
version="4.0">
|
||||
</web-app>
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package com.itheima.test;
|
||||
|
||||
import com.itheima.dao.UserDao;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class SpringTest {
|
||||
|
||||
|
||||
@Test
|
||||
//测试scope属性:
|
||||
public void test1(){
|
||||
|
||||
//获取spring容器中的context
|
||||
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||
|
||||
UserDao userDao1 = (UserDao) app.getBean("userDao");
|
||||
UserDao userDao2 = (UserDao) app.getBean("userDao");
|
||||
|
||||
//scope="singleton"
|
||||
// System.out.println(userDao1); //com.itheima.dao.impl.UserDaoImpl@711f39f9
|
||||
// System.out.println(userDao2); //com.itheima.dao.impl.UserDaoImpl@711f39f9
|
||||
|
||||
//scope="protoType"
|
||||
System.out.println(userDao1); //com.itheima.dao.impl.UserDaoImpl@4f063c0a
|
||||
System.out.println(userDao2); //com.itheima.dao.impl.UserDaoImpl@1e6d1014
|
||||
// app.close();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<parent>
|
||||
<artifactId>Spring</artifactId>
|
||||
<groupId>com.itheima</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring_ioc_anno</artifactId>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.32</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>c3p0</groupId>
|
||||
<artifactId>c3p0</artifactId>
|
||||
<version>0.9.1.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>1.1.12</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>com.itheima.test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<!--导入spring继承junit坐标-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-com.itheima.test</artifactId>
|
||||
<version>5.0.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>5.2.2.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
|
||||
version="4.0">
|
||||
</web-app>
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
package com.itheima.config;
|
||||
|
||||
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.beans.PropertyVetoException;
|
||||
|
||||
//<!-- 加载外部的properties文件-->
|
||||
//<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
|
||||
@PropertySource("classpath:jdbc.properties")
|
||||
public class DataSourceConfiguration {
|
||||
|
||||
@Value("${jdbc.driver}")
|
||||
private String driver;
|
||||
@Value("${jdbc.url}")
|
||||
private String url;
|
||||
@Value("${jdbc.username}")
|
||||
private String username;
|
||||
@Value("${jdbc.password}")
|
||||
private String password;
|
||||
|
||||
@Bean("dataSource") //Spring会将当前方法的返回值以指定名称存储到Spring容器中
|
||||
public DataSource getDataSource() throws PropertyVetoException {
|
||||
|
||||
ComboPooledDataSource dataSource=new ComboPooledDataSource();
|
||||
dataSource.setDriverClass(driver);
|
||||
dataSource.setJdbcUrl(url);
|
||||
dataSource.setUser(username);
|
||||
dataSource.setPassword(password);
|
||||
|
||||
return dataSource;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package com.itheima.config;
|
||||
|
||||
|
||||
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.*;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.beans.PropertyVetoException;
|
||||
|
||||
//标志该类是Spring的核心配置类
|
||||
|
||||
//<!--配置组件扫描-->
|
||||
//<!--包扫描的方式创建-->
|
||||
//<context:component-scan base-package="com.itheima"></context:component-scan>
|
||||
@Configuration
|
||||
@ComponentScan("com.itheima")
|
||||
//<import resource=""> 引入其他分的核心配置文件
|
||||
@Import(DataSourceConfiguration.class)
|
||||
//@Import({{DataSourceConfiguration.class,xx.class})引入多个的方式
|
||||
public class SpringConfiguration {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package com.itheima.dao;
|
||||
|
||||
public interface UserDao {
|
||||
|
||||
|
||||
public void save();
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package com.itheima.dao.impl;
|
||||
|
||||
import com.itheima.dao.UserDao;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
|
||||
//<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
|
||||
//@Component("userDao")
|
||||
@Repository("userDao")
|
||||
public class UserDaoImpl implements UserDao {
|
||||
|
||||
|
||||
public void save() {
|
||||
|
||||
System.out.println("save running 。。。");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package com.itheima.service;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
public void save();
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
package com.itheima.service.impl;
|
||||
|
||||
import com.itheima.dao.UserDao;
|
||||
import com.itheima.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
|
||||
//<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
|
||||
//@Component("userService")
|
||||
@Service("userService")
|
||||
//@Scope("prototype")
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
//可以从Spring容器中获取driver
|
||||
@Value("${jdbc.driver}")
|
||||
private String driver;
|
||||
|
||||
|
||||
//<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
|
||||
// @Autowired //单独写这个,不写Qualifier则单独按照数据类型从Spring容器中进行配置;但是如果UserDao存在多个则无法单独使用
|
||||
// @Qualifier("userDao") //Qualifier是按照id值从容器中进行匹配的,但是主要此处Qualifier结合Autowired一起使用
|
||||
@Resource(name="userDao") //相当于@Autowired+@Qualifier
|
||||
//使用注解方式,set方法可以省掉不写
|
||||
private UserDao userDao;
|
||||
|
||||
// public void setUserDao(UserDao userDao) {
|
||||
// this.userDao = userDao;
|
||||
// }
|
||||
|
||||
public void save() {
|
||||
System.out.println(driver);
|
||||
userDao.save();
|
||||
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
System.out.println("Service对象的初始化方法");
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void destroy(){
|
||||
System.out.println("Service对象的销毁方法");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,93 @@
|
|||
package com.itheima.test;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.beans.PropertyVetoException;
|
||||
import java.sql.Connection;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class DataSourceTest {
|
||||
|
||||
@Test
|
||||
//测试Spring容器产生数据源对象
|
||||
public void test4() throws Exception {
|
||||
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||
|
||||
DataSource dataSource = app.getBean(DataSource.class);
|
||||
Connection connection = dataSource.getConnection();
|
||||
System.out.println(connection);
|
||||
connection.close();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
//测试手动创建c3p0数据源(通过加载properties配置文件)
|
||||
public void test3() throws Exception {
|
||||
|
||||
//读取配置文件
|
||||
ResourceBundle resourceBundle=ResourceBundle.getBundle("jdbc"); //这里的路径相当于是类加载路径下
|
||||
|
||||
String driver=resourceBundle.getString("jdbc.driver");
|
||||
String url=resourceBundle.getString("jdbc.url");
|
||||
String username=resourceBundle.getString("jdbc.username");
|
||||
String password=resourceBundle.getString("jdbc.password");
|
||||
|
||||
//创建数据源对象,设置连接参数
|
||||
ComboPooledDataSource dataSource=new ComboPooledDataSource();
|
||||
dataSource.setDriverClass(driver);
|
||||
dataSource.setJdbcUrl(url);
|
||||
dataSource.setUser(username);
|
||||
dataSource.setPassword(password);
|
||||
|
||||
Connection connection = dataSource.getConnection();
|
||||
connection.close();
|
||||
System.out.println(connection);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
//测试手动创建druid数据源
|
||||
public void test2() throws Exception{
|
||||
DruidDataSource druidDataSource=new DruidDataSource();
|
||||
druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
|
||||
druidDataSource.setUrl("jdbc:mysql:///db1?useSSL=false");
|
||||
druidDataSource.setUsername("root");
|
||||
druidDataSource.setPassword("root");
|
||||
|
||||
DruidPooledConnection connection = druidDataSource.getConnection();
|
||||
System.out.println(connection);
|
||||
|
||||
connection.close();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
//测试手动创建c3p0数据源
|
||||
public void test1() throws Exception {
|
||||
ComboPooledDataSource dataSource=new ComboPooledDataSource();
|
||||
|
||||
dataSource.setDriverClass("com.mysql.jdbc.Driver");
|
||||
|
||||
dataSource.setJdbcUrl("jdbc:mysql:///db1?useSSL=false");
|
||||
dataSource.setUser("root");
|
||||
dataSource.setPassword("root");
|
||||
|
||||
//获取资源
|
||||
Connection connection = dataSource.getConnection();
|
||||
System.out.println(connection);
|
||||
connection.close();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue