前言
没有制作或是没有计划制作详细操作的作业内容将在 快截止提交时 发布在此
本文为作业的仅代码展示,不再有详细步骤指导
你可以在确保项目文件正常创建的情况下拷贝所有代码实现作业内容
7 chapterlab04
chapterlab04极速版
7-1 创建项目
在 proj
中新建Maven项目 chapterlab04
在 src/main/java
新建如下软件包
cn.fvti.chapterlab04.domain
cn.fvti.chapterlab04.mapper
cn.fvti.chapterlab04.utils
在 src/main/resources
新建如下资源文件夹
cn/fvti/chapterlab04/mapper
在 src/test/java
新建如下测试软件包
cn.fvti.chapterlab04
配置 pom.xml
在合适位置添加如下配置
<dependencies>
<!--1. MySQL 驱动-->
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
<!--2. mybatis 依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.16</version>
</dependency>
<!--3. junit-->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--4. log4j-->
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--5. lombok-->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>
</dependencies>
在 resources
资源目录下新建文件 db.properties
添加如下代码
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/chapterlab04?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
mysql.username=root
mysql.password=123456
在 resources
资源目录下新建文件 log4j.xml
添加如下代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS}%m
(%F:%L) \n" />
</layout>
</appender>
<logger name="java.sql">
<level value="debug" />
</logger>
<logger name="org.apache.ibatis">
<level value="info" />
</logger>
<root>
<level value="debug" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
在 resources
资源目录下新建文件 mybatis-config.xml
添加如下代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 环境配置 -->
<!-- 加载类路径下的属性文件 -->
<properties resource="db.properties"/>
<!-- 启用全局驼峰转换 -->
<!-- <settings>-->
<!-- <setting name="mapUnderscoreToCamelCase" value="true"/>-->
<!-- </settings>-->
<typeAliases>
<!--告诉mybatis,实体类的路径-->
<package name="cn.fvti.chapterlab04.domain"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 数据库连接相关配置 ,db.properties文件中的内容-->
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
<environment id="prod">
<transactionManager type="JDBC"/>
<!-- 数据库连接相关配置 ,db.properties文件中的内容-->
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="admin"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<!-- mapping文件路径配置 -->
<mappers>
<!--扫包:要求 1. mapper接口和映射xml文件必须拥有相同的目录结构 2. mapper接口和映射xml文件的名字必须是一致-->
<package name="cn.fvti.chapterlab04.mapper"/>
</mappers>
</configuration>
7-2 Sql导入
使用Navicat连接数据库创建 chapterlab04
数据库,字符集 utf8mb4
右键刚刚创建好的数据库选择 运行SQL文件 运行chapterlab04.sql
以导入数据
7-3 完善代码
在 cn.fvti.chapterlab04.domain
软件包下新建 Course
Java类添加如下代码
package cn.fvti.chapterlab04.domain;
import lombok.Data;
@Data
public class Course {
/**
* 主键
*/
private Integer id;
/**
* 课程名称
*/
private String name;
/**
* 授课老师
*/
private String teacher;
/**
* 上课教室
*/
private String room;
/**
* 课时
*/
private Integer classHour;
}
在 cn.fvti.chapterlab04.domain
软件包下新建 Student
Java类添加如下代码
package cn.fvti.chapterlab04.domain;
import lombok.Data;
import java.util.List;
@Data
public class Student {
/**
* 主键id
*/
public Integer id;
/**
* 学号
*/
private String stuId;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 学生职位
*/
private String position;
/**
* 性别
*/
private String sex;
private List<Course> courses;
}
在 cn.fvti.chapterlab04.utils
软件包下新建 MyBatisUtils
Java类添加如下代码
package cn.fvti.chapterlab04.utils;
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 java.io.Reader;
public class MyBatisUtils {
private static SqlSessionFactory sqlSessionFactory = null;
// 初始化SqlSessionFactory对象
static {
try {
// 使用MyBatis提供的Resources类加载MyBatis的配置文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
// 构建SqlSessionFactory工厂
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
// 获取SqlSession对象的静态方法
public static SqlSession getSession(){
return sqlSessionFactory.openSession(true);
}
}
在 cn.fvti.chapterlab04.mapper
软件包下新建 StudentMapper
Java接口 添加如下代码
package cn.fvti.chapterlab04.mapper;
import cn.fvti.chapterlab04.domain.Student;
public interface StudentMapper {
public Student getStudentAndCourse(Student student);
}
在resources资源目录 cn/yaklo/chapterlab04/mapper
下新建文件 StudentMapper.xml
添加如下代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!-- mapper为映射的根节点 -->
<!-- mapper为映射的根节点,namespace指定mapper接口的全限定类名
mybais会根据这个接口动态创建一个实现类去实现这个接口,
而这个实现类是一个Mapper对象 -->
<mapper namespace="cn.fvti.chapterlab04.mapper.StudentMapper">
<resultMap id="stuMap" type="cn.fvti.chapterlab04.domain.Student">
<id property="id" column="id"/>
<result property="stuId" column="stu_id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="position" column="position"/>
<result property="sex" column="sex"/>
<collection property="courses" ofType="cn.fvti.chapterlab04.domain.Course">
<id property="id" column="cid"/>
<result property="name" column="cname"/>
<result property="teacher" column="teacher"/>
<result property="room" column="room"/>
<result property="classHour" column="class_hour"/>
</collection>
</resultMap>
<select id="getStudentAndCourse" resultMap="stuMap">
SELECT stu.*,
course.id cid,
course.name cname,
course.teacher,
course.room,
course.class_hour
FROM stu
LEFT JOIN stu_course sc ON stu.stu_id = sc.stu_id
LEFT JOIN course ON sc.course_id = course.id
WHERE stu.stu_id = #{stuId};
</select>
</mapper>
7-4 测试项目
在test测试文件夹下的 cn.fvti.chapterlab04
软件包中新建 MyBatisTest
Java类添加如下代码
package cn.fvti.chapterlab04;
import cn.fvti.chapterlab04.domain.Student;
import cn.fvti.chapterlab04.mapper.StudentMapper;
import cn.fvti.chapterlab04.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class MyBatisTest {
@Test
public void getStudentAndCourse(){
// 通过工具类生成SqlSession对象
SqlSession session = MyBatisUtils.getSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = new Student();
student.setStuId("20225800");
Student res = mapper.getStudentAndCourse(student);
System.out.println(res);
//释放资源
session.close();
}
}
自行测试
8 chapterlab05
功能测试 👇
视频存储于OneDrive,能否播放比较看脸
创建StudentMapper正常操作应该是使用接口创建,视频后面测试类操作数据库出现乱码是我电脑设置出问题了
8-1 创建项目
在 proj
中新建Maven项目 chapterlab05
在 src/main/java
新建如下软件包
cn.fvti.chapterlab05.domain
cn.fvti.chapterlab05.mapper
在 src/test/java
新建如下测试软件包
cn.fvti.chapterlab05
配置 pom.xml
在合适位置添加如下配置
<dependencies>
<!--1. MySQL 驱动-->
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
<!--2. mybatis 依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.16</version>
</dependency>
<!--3. junit-->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--4. log4j-->
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--5. lombok-->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>
</dependencies>
在 resources
资源目录下新建文件 db.properties
添加如下代码
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/chapterlab05?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
mysql.username=root
mysql.password=123456
在 resources
资源目录下新建文件 log4j.xml
添加如下代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS}%m
(%F:%L) \n" />
</layout>
</appender>
<logger name="java.sql">
<level value="debug" />
</logger>
<logger name="org.apache.ibatis">
<level value="info" />
</logger>
<root>
<level value="debug" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
在 resources
资源目录下新建文件 mybatis-config.xml
添加如下代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 环境配置 -->
<!-- 加载类路径下的属性文件 -->
<properties resource="db.properties"/>
<!-- 启用全局驼峰转换 -->
<!-- <settings>-->
<!-- <setting name="mapUnderscoreToCamelCase" value="true"/>-->
<!-- </settings>-->
<typeAliases>
<!--告诉mybatis,实体类的路径-->
<package name="cn.fvti.chapterlab05.domain"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 数据库连接相关配置 ,db.properties文件中的内容-->
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
<environment id="prod">
<transactionManager type="JDBC"/>
<!-- 数据库连接相关配置 ,db.properties文件中的内容-->
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="admin"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<!-- mapping文件路径配置 -->
<mappers>
<!--扫包:要求 1. mapper接口和映射xml文件必须拥有相同的目录结构 2. mapper接口和映射xml文件的名字必须是一致-->
<package name="cn.fvti.chapterlab05.mapper"/>
</mappers>
</configuration>
8-2 Sql导入
使用Navicat连接数据库创建 chapterlab05
数据库,字符集 utf8mb4
右键刚刚创建好的数据库选择 运行SQL文件 运行chapterlab05.sql
以导入数据
8-3 完善代码
在 cn.fvti.chapterlab05.domain
软件包下新建 Student
Java类添加如下代码
package cn.fvti.chapterlab05.domain;
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
private int age;
private int cid;
private int no;
private String hometown;
}
在 cn.fvti.chapterlab05.mapper
软件包下新建 StudentMapper
Java接口 添加如下代码
package cn.fvti.chapterlab05.mapper;
import cn.fvti.chapterlab05.domain.Student;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
public interface StudentMapper {
@Select("select * from stu_details where no = #{no}")
Student selectStudentById(int no);
@Insert("insert into stu_details(name,age,cid,no,hometown)values(#{name},#{age},#{cid},#{no},#{hometown})")
Integer insertStudentById(Student student);
@Update("update stu_details set name = #{name}, age = #{age}, cid = #{cid}, hometown = #{hometown} where no = #{no}")
Integer updataStudentById(Student student);
@Delete("delete from stu_details where no = #{no}")
Integer deleteStudentById(int no);
}
8-4 测试项目
在test测试文件夹下的 cn.fvti.chapterlab05
软件包中新建 MyBatisTest
Java类添加如下代码
package cn.fvti.chapterlab05;
import cn.fvti.chapterlab05.domain.Student;
import cn.fvti.chapterlab05.mapper.StudentMapper;
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.Reader;
public class MyBatisTest {
/**
* 用注解的方式,实现学生查询
*/
@Test
public void selectStudentById(){
try{
// 1. 读取mybatis核心文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
// 2. 创建SqlSeessionFactoryBuilder
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// 3. 创建SqlSessionFactory
SqlSessionFactory sessionFactory = builder.build(reader);
// 4. 拿到SqlSession
SqlSession session = sessionFactory.openSession(true);
// 5. 获取代理实现类,进行数据库的操作
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
int no = 2022004;
Student student = studentMapper.selectStudentById(no);
System.out.println(student);
//释放资源
session.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 用注解的方式,实现学生添加
*/
@Test
public void insertStudentById(){
try{
// 1. 读取mybatis核心文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
// 2. 创建SqlSeessionFactoryBuilder
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// 3. 创建SqlSessionFactory
SqlSessionFactory sessionFactory = builder.build(reader);
// 4. 拿到SqlSession
SqlSession session = sessionFactory.openSession(true);
// 5. 获取代理实现类,进行数据库的操作
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
Student student = new Student();
student.setName("虚之亚克洛");
student.setAge(19);
student.setCid(1);
student.setNo(2023001);
student.setHometown("福建福州");
int students = studentMapper.insertStudentById(student);
System.out.println(students);
//释放资源
session.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 用注解的方式,实现学生修改
*/
@Test
public void updataStudentById(){
try{
// 1. 读取mybatis核心文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
// 2. 创建SqlSeessionFactoryBuilder
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// 3. 创建SqlSessionFactory
SqlSessionFactory sessionFactory = builder.build(reader);
// 4. 拿到SqlSession
SqlSession session = sessionFactory.openSession(true);
// 5. 获取代理实现类,进行数据库的操作
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
Student student = new Student();
student.setName("钱七");
student.setAge(19);
student.setCid(3);
student.setNo(2022004);
student.setHometown("杭州");
int students = studentMapper.updataStudentById(student);
System.out.println(students);
//释放资源
session.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 用注解的方式,实现学生删除
*/
@Test
public void deleteStudentById(){
try{
// 1. 读取mybatis核心文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
// 2. 创建SqlSeessionFactoryBuilder
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// 3. 创建SqlSessionFactory
SqlSessionFactory sessionFactory = builder.build(reader);
// 4. 拿到SqlSession
SqlSession session = sessionFactory.openSession(true);
// 5. 获取代理实现类,进行数据库的操作
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
int no = 2022002;
int student = studentMapper.deleteStudentById(no);
System.out.println(student);
//释放资源
session.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
自行测试
9 chapterlab06
功能测试 👇
视频存储于OneDrive,能否播放比较看脸
视频后面测试类运行出现乱码是我电脑设置的问题,正常不会乱码
9-1 创建项目
在 proj
中新建Maven项目 chapterlab06
在 src/main/java
新建如下软件包
cn.fvti.chapterlab06.dao
cn.fvti.chapterlab06.service
配置 pom.xml
在合适位置添加如下配置
<dependencies>
<!--1. MySQL 驱动-->
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
<!--2. mybatis 依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.16</version>
</dependency>
<!--3. junit-->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--4. log4j-->
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--5. lombok-->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>
<!-- 6. spring framework-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.25.RELEASE</version>
</dependency>
</dependencies>
9-2 完善代码
9-4-1 Dao
在 cn.fvti.chapterlab05.dao
软件包下新建 UserDao
Java接口 添加如下代码
package cn.fvti.chapterlab06.dao;
public interface UserDao {
public boolean login(String username, String password);
}
在 cn.fvti.chapterlab06.dao
软件包下新建 impl.UserDaoImpl
Java类添加如下代码
package cn.fvti.chapterlab06.dao.impl;
import cn.fvti.chapterlab06.dao.UserDao;
public class UserDaoImpl implements UserDao {
@Override
public boolean login(String username, String password) {
if ("张三".equals(username) && "123456".equals(password)) {
return true;
}
return false;
}
}
9-4-2 Service
在 cn.fvti.chapterlab06.service
软件包下新建 UserService
Java接口 添加如下代码
package cn.fvti.chapterlab06.service;
public interface UserService {
public boolean login(String username, String password);
}
在 cn.fvti.chapterlab06.service
软件包下新建 impl.UserServiceImpl
Java类添加如下代码
package cn.fvti.chapterlab06.service.impl;
import cn.fvti.chapterlab06.dao.UserDao;
import cn.fvti.chapterlab06.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public boolean login(String username, String password) {
return userDao.login(username, password);
}
}
9-4-3 Xml
在 resources
资源目录下新建文件 applicationContext.xml
添加如下代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDao" class="cn.fvti.chapterlab06.dao.impl.UserDaoImpl"/>
<bean id="userService" class="cn.fvti.chapterlab06.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
</beans>
9-3 测试项目
在test测试文件夹下新建 ChapterLab06Test
Java类添加如下代码
import cn.fvti.chapterlab06.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ChapterLab06Test {
@Test
public void testLogin() {
// 1. 获取IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 从IOC容器中获取UserService对象:按照类型获取UserService对象
// (注意:由于UserServiceImpl是实现UserService,所以最终找到唯一的bean)
UserService userService = ctx.getBean(UserService.class);
// 3. 调用login方法
boolean flag = userService.login("张三", "123456");
if (flag) {
System.out.println("登录成功");
} else {
System.out.println("登录失败");
}
}
}
自行测试