SSM练习

前言

每个周上课都布置个作业,正好当做练习题做一个记录

Mybatis

Week1(增删改查)

要求

  • 1、UserMapper.xml的代码,这个映射文件的所有放在一幅图中
  • 2、添加一条记录的执行结果,截图包含UserTest添加记录代码和console执行结果及新添加到数据库的那一条记录,放在一个屏幕中截图
  • 3、更新一条记录的执行结果,截图要求同2
  • 4、删除一条记录的执行结果,截图要求同2
  • 5、查询所有用户记录的执行结果,要求输出所有的记录(含uid,uname,uage),截图要求同2

User类

package com.sentiment.pojo;

public class User {
   private Integer uid;

   private String uname;

   private Integer uage;

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", uname='" + uname + '\'' +
                ", uage=" + uage +
                '}';
    }

    public User() {
    }

    public User(Integer uid, String uname, Integer uage) {
        this.uid = uid;
        this.uname = uname;
        this.uage = uage;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public Integer getUage() {
        return uage;
    }

    public void setUage(Integer uage) {
        this.uage = uage;
    }
}

接口

int insertUser();

int updateUser();

int deleteUser();

List<User> getUserList();

配置

分别代表插入、修改、删除、查询全部四个功能

<insert id="insertUser">
    insert into users values(3,'zjx',18);
</insert>

<update id="updateUser">
    update users set uname='zjx' where uid=3;
</update>

<delete id="deleteUser">
    delete from users where uid=3
</delete>


<select id="getUserList" resultType="user">
    select * from users;
</select>

测试

import com.sentiment.mapper.UserMapper;
import com.sentiment.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 UserTest {
    @Test
    public void insertUser() throws IOException {
        InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = sqlSessionFactoryBuilder.build(is);
        SqlSession sqlSession = build.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.insertUser();
    }

    @Test
    public void updateUser() throws IOException {
        InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = sqlSessionFactoryBuilder.build(is);
        SqlSession sqlSession = build.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateUser();
    }

    @Test
    public void deleteUser() throws IOException {
        InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = sqlSessionFactoryBuilder.build(is);
        SqlSession sqlSession = build.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteUser();
    }
    @Test
    public void getUserList() throws IOException {
        InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = sqlSessionFactoryBuilder.build(is);
        SqlSession sqlSession = build.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();
        userList.forEach(System.out::println);
    }
}

Week2(动态SQL)

  • 1、查询1:当uname不为空时,用uname模糊查询;当uage不为空时,用uage查询;在映射文件中用if元素实现
  • 2、查询2:当uname不为空时,用uname模糊查询;当uname为空时,当uage不为空时,用uage查询;两者都为空时,查询uage大于0的;在映射文件中用choose元素实现
  • 3、第1个查询,使用where元素
  • 4、第2个查询,使用trim元素
  • 5、更新操作,使用set元素实现
  • 6、写测试类,分别写三个有Test注解的方法,实现查询1,查询2和更新

还是User类

接口

List<User> selectOne(@Param("uname") String uname,@Param("uage") Integer uage);

List<User> selectTwo(@Param("uname") String uname,@Param("uage") Integer uage);

int setTest(@Param("uname") String uname,@Param("uid") Integer uid);

配置文件

    <select id="selectOne" resultType="user">
        select * from users
        <where>
        <if test="uname != null and uname !=''">
            uname like '%${uname}%'
        </if>
        <if test="uage !=null and uage !=''">
            and uage like '%${uage}%'
        </if>
        </where>
    </select>

    <select id="selectTwo" resultType="user">
        select * from users
        <trim prefix="where" >
            <choose>
                <when test="uname != null and uname !=''">
                    uname like '%${uname}%'
                </when>
                 <when test="uage !=null and uage !=''">
                     uage like '%${uage}%'
                 </when>
                 <when test="uname ==null and uage==null and uname='' and uage=''">
                     uage > 1
                 </when>
            </choose>
        </trim>
    </select>

    <update id="setTest">
        update users
        <set>
            uname =#{uname}
        </set>
        where uid = #{uid}
    </update>

测试

import com.sentiment.mapper.UserMapper;
import com.sentiment.pojo.User;
import com.sentiment.utils.SqlSeesionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class SelectTest {
    @Test
    public void selectOneTest(){
        SqlSession sqlSession = SqlSeesionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> resault = mapper.selectOne("刘",1);
        resault.forEach(System.out::println);
    }
    @Test
    public void selectTwoTest(){
        SqlSession sqlSession = SqlSeesionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> resault = mapper.selectTwo("刘", 1);
        resault.forEach(System.out::println);
    }
    @Test
    public void setTest(){
        SqlSession sqlSession = SqlSeesionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int resault = mapper.setTest("lhy", 3);
        System.out.println(resault);
    }
}

Week3(association多对一查询)

基于群里的数据表college和student,模仿老师做的操作,实现从student查询,带出college的操作:

在这里插入图片描述

  • 1、将两个表的结构导入数据库;
  • 2、创建对应的两个实体类,在student中加上College类型的属性;
  • 3、创建映射文件,在CollegeMapper.xml中加一个根据id查询的select元素,在StudentMapper.xml中加上一个根据stuNo查询的select元素;在该映射文件中再加上一个resultMap元素,使用association元素实现关联映射,实现嵌套查询;
  • 4、写测试类、测试方法实现,输出sql语句和学生及学院信息

College类

package com.sentiment.pojo;

public class College {
    private int id;

    private String coName;

    private String temp;

    @Override
    public String toString() {
        return "College{" +
                "id=" + id +
                ", coName='" + coName + '\'' +
                ", temp='" + temp + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCoName() {
        return coName;
    }

    public void setCoName(String coName) {
        this.coName = coName;
    }

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public College() {
    }

    public College(int id, String coName, String temp) {
        this.id = id;
        this.coName = coName;
        this.temp = temp;
    }
}

Student类

由于要实现多对一查询所以,添加了一个College类型的属性 private College college;

package com.sentiment.pojo;

public class Student {

    private int stuNo;

    private int coId;

    private String stuName;

    private String enterDate;

    private String temp;

    private College college;

    @Override
    public String toString() {
        return "Student{" +
                "stuNo=" + stuNo +
                ", coId=" + coId +
                ", stuName='" + stuName + '\'' +
                ", enterDate='" + enterDate + '\'' +
                ", temp='" + temp + '\'' +
                ", college=" + college +
                '}';
    }

    public College getCollege() {
        return college;
    }

    public void setCollege(College college) {
        this.college = college;
    }

    public Student(int stuNo, int coId, String stuName, String enterDate, String temp, College college) {
        this.stuNo = stuNo;
        this.coId = coId;
        this.stuName = stuName;
        this.enterDate = enterDate;
        this.temp = temp;
        this.college = college;
    }

    public int getStuNo() {
        return stuNo;
    }

    public void setStuNo(int stuNo) {
        this.stuNo = stuNo;
    }

    public int getCoId() {
        return coId;
    }

    public void setCoId(int coId) {
        this.coId = coId;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public String getEnterDate() {
        return enterDate;
    }

    public void setEnterDate(String enterDate) {
        this.enterDate = enterDate;
    }

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public Student() {
    }

    public Student(int stuNo, int coId, String stuName, String enterDate, String temp) {
        this.stuNo = stuNo;
        this.coId = coId;
        this.stuName = stuName;
        this.enterDate = enterDate;
        this.temp = temp;
    }
}

接口

//Student类的接口
Student selectOne(@Param("stuNo") int stuNo);  //select根据stuNo查询
Student associationTest(@Param("stuNo") int stuNo); //association多对一查询

//College类的接口
College selectOne(@Param("id") int id);		//select根据id查询

配置文件

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 namespace="com.sentiment.mapper.StudentMapper">

    <select id="selectOne" resultType="student">
        select * from student where stuNo=#{stuNo}
    </select>


    <resultMap id="associationTest" type="Student">
        <id column="stuNo" property="stuNo"></id>
        <result column="coId" property="coId"></result>
        <result column="stuName" property="stuName"></result>
        <result column="enterDate" property="enterDate"></result>
        <result column="temp" property="temp"></result>
        <association property="college" javaType="College">
            <id column="id" property="id"></id>
            <result column="coName" property="coName"></result>
            <result column="temp" property="temp"></result>
        </association>
    </resultMap>

    <select id="associationTest" resultMap="associationTest">
        select * from student left join college on student.coId = college.id where student.stuNo=#{stuNo}
    </select>
</mapper>

CollegeMapper.xml

<select id="selectOne" resultType="college">
    select * from college where id=#{id}
</select>

测试

import com.sentiment.mapper.CollegeMapper;
import com.sentiment.mapper.StudentMapper;
import com.sentiment.pojo.College;
import com.sentiment.pojo.Student;
import com.sentiment.utils.SqlSeesionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class AssociationTest {
    @Test
    public void CollegeSelectOne(){
        SqlSession sqlSession = SqlSeesionUtils.getSqlSession();
        CollegeMapper mapper = sqlSession.getMapper(CollegeMapper.class);
        College colleges = mapper.selectOne(1);
        System.out.println(colleges);
    }
    @Test
    public void StudentSelectOne(){
        SqlSession sqlSession = SqlSeesionUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student students = mapper.selectOne(2);
        System.out.println(students);
    }
    @Test
    public void AssociationTest(){
        SqlSession sqlSession = SqlSeesionUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.associationTest(1);
        System.out.println(student);
    }
}

执行结果

在这里插入图片描述

Week4(二级缓存)

要求

  • 实现学院college对学生student的一对多查询,配置好二级缓存。
  • 1、在StudentMapper中添加一个select元素,sql语句的功能是根据coId查询所有的学生。
  • 2、在pojo实体类College中添加一个List<Student>属性,命名为stuList
  • 3、在CollegeMapper中添加一个resultMap,里面配置好collection元素,collection元素的select属性设置为第1步设置的那个select元素的id,不要忘记加上命名空间,collection元素的property属性设置为和第2步一样的名字stuList
  • 4、在CollegeMapper中添加一个select元素,sql语句的功能是根据主键id查询一条学院信息,该select元素的设置属性resultMap为第3步设置好的那个resultMap的id属性值
  • 5、在mabatis-config中添加二级缓存的配置,在collegeMapper和StudentMapper中添加上缓存配置
  • 6、测试成功后,截图提交,mybatis-config.xml、CollegeMapper.xml和StudentMapper.xml源代码,截全

collection查询

College类

看了看要求感觉应该用collection的分布查询来做,但是collection都已经忘了所以先用collection做了一遍(未带二级缓存)

package com.sentiment.pojo;

import java.util.List;

public class College {
    private int id;

    private String coName;

    private String temp;

    private Student student;
    

    public College(int id, String coName, String temp, Student student) {
        this.id = id;
        this.coName = coName;
        this.temp = temp;
        this.student = student;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCoName() {
        return coName;
    }

    public void setCoName(String coName) {
        this.coName = coName;
    }

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public College() {
    }

    @Override
    public String toString() {
        return "College{" +
                "id=" + id +
                ", coName='" + coName + '\'' +
                ", temp='" + temp + '\'' +
                ", student=" + student +
                '}';
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
}

接口

public interface CollegeMapper {

    College selectOne(@Param("id") int id);

    College selectOneById(@Param("id") int id);

}

CollegeMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sentiment.mapper.CollegeMapper">
    <cache/>

    <resultMap id="selectBycoId" type="College">
        <id column="id" property="id"></id>
        <result column="coName" property="coName"></result>
        <result column="temp" property="temp"></result>
        <collection property="student" ofType="Student">
            <id column="stuNo" property="stuNo"></id>
            <result column="coId" property="coId"></result>
            <result column="stuName" property="stuName"></result>
            <result column="enterDate" property="enterDate"></result>
            <result column="temp" property="temp"></result>
        </collection>
    </resultMap>

    <select id="selectOneById" resultMap="selectBycoId">
        select * from college left join student on  college.id = student.coId where college.id = #{id}
    </select>
</mapper>

测试

@Test
public void getAllByStepOne() throws IOException {
    InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
    SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
    SqlSessionFactory build = sqlSessionFactoryBuilder.build(is);
    SqlSession sqlSession1 = build.openSession(true);
    CollegeMapper mapper1 = sqlSession1.getMapper(CollegeMapper.class);
    College allByStepOne1 = mapper1.getAllByStepOne(1);
    System.out.println(allByStepOne1);
}

collection分布查询

了解collection后再用分布查询的方式做一遍

College类

按要求添加了:

private List<Student> stuList;

除此外由于需要实现二级缓存,所以要实现Serializable接口

接口

//StudentMapper
List<Student> selectBycoId(@Param("coId") int coId);

//CollegeMapper
College getAllByStepOne(@Param("id") int id);

配置文件

二级缓存选项默认是开启的,但是奈何要求截图,所以就加到了mybatis-config.xml

<!-- StudentMapper.xml -->
<cache/>
<select id="selectBycoId" resultType="student">
    select * from student where coId=#{coId}
</select>


<!-- CollegeMapper.xml -->
<cache/>
<resultMap id="AllByStepOne" type="College">
    <id column="id" property="id"></id>
    <result column="coName" property="coName"></result>
    <result column="temp" property="temp"></result>
    <collection property="stuList"
                select="com.sentiment.mapper.StudentMapper.selectBycoId"
                column="id">
    </collection>
</resultMap>

<select id="getAllByStepOne" resultMap="AllByStepOne">
    select * from college where id = #{id}
</select>


<!-- mybatis-config.xml-->
<!-- 二级缓存 -->
<settings>
    <setting name="cacheEnabled" value="true"/>
</settings>

测试

由于需要使用二级缓存,而二级缓存是sqlSession级别的,所以没有使用工具类,

    @Test
    public void getAllByStepOne() throws IOException {
        InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = sqlSessionFactoryBuilder.build(is);
        SqlSession sqlSession1 = build.openSession(true);
        CollegeMapper mapper1 = sqlSession1.getMapper(CollegeMapper.class);
        College allByStepOne1 = mapper1.getAllByStepOne(1);
        System.out.println(allByStepOne1);

        sqlSession1.close();
        SqlSession sqlSession2 = build.openSession(true);
        CollegeMapper mapper2 = sqlSession2.getMapper(CollegeMapper.class);
        College allByStepOne2 = mapper2.getAllByStepOne(1);
        System.out.println(allByStepOne2);
        sqlSession2.close();
    }
}

在这里插入图片描述

Week5(注解开发)

第一次接触注解开发,感觉确实方便很多,连xml配置文件都不用写了。

  • 1、实现student单表的增删改查
  • 2、实现从student到college的一对一查询
  • 3、实现从college到student的一对多查询
  • 截图提交:StudentMapper源代码,CollegeMapper源代码,mybatis-config.xml对于mapper接口文件的引入的代码
  • Student和College的实体类属性部分截图

实体类还是之前那两个StudentCollege

StudentMapper

package com.sentiment.mapper;

import com.sentiment.pojo.Student;
import org.apache.ibatis.annotations.*;
public interface StudentMapper1 {
    @Insert("insert into student values(3,1,'王五','2000-10-01',null)")
    void insertStudent();

    @Delete("delete from student where stuNo=3")
    void deleteStudent();

    @Update("update student set stuName='Sentiment' where stuNo=1")
    void updateStudent();

    @Select("select * from student where coId=#{coId}")
    Student selectStudent(int coId);

    @Select("select * from student where coId=#{coId} and stuNo=1")
    @Results({@Result(column = "coId" ,property = "college",
            one = @One(select = "com.sentiment.mapper.CollegeMapper1.selectCollege")
    )})
    Student selectStudentByCoId(int coId);

    @Select("select * from student where coId=#{coId}")
    @Results({@Result(id = true,column = "coId",property = "coId")})
    Student selectToCollege(int coId);
}

CollegeMapper

package com.sentiment.mapper;

import com.sentiment.pojo.College;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

public interface CollegeMapper1 {
    @Select("select * from college where id=#{id}")
    @Results({@Result(id = true,column = "id",property = "id"),
        @Result(column = "coName",property = "coName"),
        @Result(column = "temp",property = "temp"),
        @Result(column = "id",property = "stuList",
                many = @Many(select =
                        "com.sentiment.mapper.StudentMapper1.selectToCollege"))
    })
    College selectCollege(int id);
}

测试

import com.sentiment.mapper.CollegeMapper1;
import com.sentiment.mapper.StudentMapper1;
import com.sentiment.pojo.College;
import com.sentiment.pojo.Student;
import com.sentiment.utils.SqlSeesionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class TestAnnotation {
    @Test
    public void InsertStudent(){
        SqlSession sqlSession = SqlSeesionUtils.getSqlSession();
        StudentMapper1 mapper = sqlSession.getMapper(StudentMapper1.class);
        mapper.insertStudent();
    }

    @Test
    public void DeleteStudent(){
        SqlSession sqlSession = SqlSeesionUtils.getSqlSession();
        StudentMapper1 mapper = sqlSession.getMapper(StudentMapper1.class);
        mapper.deleteStudent();
    }

    @Test
    public void UpdateStudent(){
        SqlSession sqlSession = SqlSeesionUtils.getSqlSession();
        StudentMapper1 mapper = sqlSession.getMapper(StudentMapper1.class);
        mapper.updateStudent();
    }

    @Test
    public void SelectStudent(){
        SqlSession sqlSession = SqlSeesionUtils.getSqlSession();
        StudentMapper1 mapper = sqlSession.getMapper(StudentMapper1.class);
        Student student = mapper.selectStudent(1);
        System.out.println(student);
    }
    @Test
    public void selectAllByStudent(){
        SqlSession sqlSession = SqlSeesionUtils.getSqlSession();
        StudentMapper1 mapper = sqlSession.getMapper(StudentMapper1.class);
        Student student = mapper.selectStudentByCoId(1);
        System.out.println(student);
    }
    @Test
    public void selectAllByCollege(){
        SqlSession sqlSession = SqlSeesionUtils.getSqlSession();
        CollegeMapper1 mapper = sqlSession.getMapper(CollegeMapper1.class);
        College college = mapper.selectCollege(1);
        System.out.println(college);
    }

}

Spring

Week6

  • 创建一个maven项目,groupId自定义,artifactId命名为springTest,在src/java下创建一个resources文件夹,将该文件夹设置为source folder。
  • 在resources下创建一个applicationContext.xml文件,文件根节点从发在群里面的压缩文件中的docs文件夹中找,按照课本86页内容寻找。
  • 其余实验步骤依据课本第90页到92页的内容来做,实现依赖注入的应用,使用setter方式。
  • 截图:项目目录展开截图,示例如下图;applicationContext.xml源代码截图。

比较简单就不写了对这课本敲就行了。。。。。

记录一下基于注解的自动装配方式,简化xml中的配置操作

applicationContext.xml
设置扫描范围

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns: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 https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.sentiment"></context:component-scan>
</beans>

UserServiceImpl类
类上添加Service注解将类标识为业务层组件,便于扫描器扫描,再在成员变量上添加一个@Autowired自动装配

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public boolean login(String name, String password) {
        return userDao.login(name,password);
    }
}

UserDaoImpl类
与上同理,@Repository:将类标识为持久层组件

@Repository
public class UserDaoImpl implements UserDao {
    @Override
    public boolean login(String name, String password) {
        if(name.equals("张三")&&password.equals("123")){
            return true;
        }
        return false;
    }
}

测试类

import com.sentiment.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    @Test
    public void testSpring(){
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService bean = ioc.getBean(UserService.class);
        boolean flag = bean.login("张三", "123");
        if (flag){
            System.out.println("登陆成功");
        }else{
            System.out.println("登录失败");
        }
    }
}

Week7(基于注解装配)

基于注解的装配,参考课本P102,7.5.2部分

  • 1、在上次作业的基础上,继续做本次实验,先在pom.xml中添加aop依赖,注意spring的版本
  • 2、在原有的applicationContext.xml文件内容中,添加beans元素的约束,添加context:component-scan元素
  • 3、添加包entity,创建类User,使用注解实现注入
  • 4、添加包dao,创建接口UserDao,在该包中添加类UserDaoImpl,实现具体代码
  • 5、添加包service,创建接口UserService,在该包中添加类UserServiceImpl,实现具体代码
  • 6、添加包controller,创建类UserController
  • 7、在项目自带包里面添加测试类,并测试
  • 截图:pom.xml,applicationContext.xml,User类,UserDao类,UserController类,运行结果
    -按照如上顺序提交截图,共7幅图

还是照着课本敲就行了,不过这里说明需要导入aop包,但是其实并没有用到aop思想,并且即使用到了,导入包也应该是如下其中一个才对(有点误导.....)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.1</version>

<!---->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.6</version>
</dependency>

Week8

根据11月18日所讲述内容,实现用户登陆验证的功能,要求在18日发在群里的chapter09项目里面做即可,步骤参考模仿ppt里面的说明,项目中具体文件命名和包命名可以自己决定,做到配置和实际一致即可。

详细要求:截图如下

  • 1、配置文件applicationContext……内容截图。
  • 2、关于用户查询的dao类的内容截图
  • 3、测试运行结果截图。

后边一直没听,一看布置的作业直接开始用JDBC了
配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 1配置数据源 -->
    <bean id="dataSource" class=
            "org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!--数据库驱动 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <!--连接数据库的url -->
        <property name="url" value="jdbc:mysql://localhost:3306/spring?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false" />
        <!--连接数据库的用户名 -->
        <property name="username" value="root" />
        <!--连接数据库的密码 -->
        <property name="password" value="123456" />
    </bean>
    <!-- 2配置JDBC模板 -->
    <bean id="jdbcTemplate"
          class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- 默认必须使用数据源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!--定义id为accountDao的Bean-->
    <bean id="StudentDao" class="com.itheima.dao.Impl.StudentDaoImpl">
        <!-- 将jdbcTemplate注入到accountDao实例中 -->
        <property name="jdbcTemplate" ref="jdbcTemplate" />
    </bean>
    <!-- 4.事务管理器,依赖于数据源 -->
    <bean id="transactionManager" class=
            "org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 5.编写通知:对事务进行增强(通知),需要编写对切入点和具体执行事务细节 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- name:*表示任意方法名称 -->
            <tx:method name="*" propagation="REQUIRED"
                       isolation="DEFAULT" read-only="false" />
        </tx:attributes>
    </tx:advice>
    <!-- 6.编写aop,让spring自动对目标生成代理,需要使用AspectJ的表达式 -->
    <aop:config>
        <!-- 切入点 -->
        <aop:pointcut expression="execution(* com.itheima.*.*(..))"
                      id="txPointCut" />
        <!-- 切面:将切入点与通知整合 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
    </aop:config>
</beans>

Dao实现类

package com.itheima.dao.Impl;
import com.itheima.dao.StudentDao;
import com.itheima.entity.Student;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
public class StudentDaoImpl implements StudentDao {
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    public List<Student> findAllStudent() {
        String sql = "select * from student";
        RowMapper<Student> rowMapper =
                new BeanPropertyRowMapper<Student>(Student.class);
        // 执行静态的SQL查询,并通过RowMapper返回结果
        return this.jdbcTemplate.query(sql, rowMapper);
    }
}

Controller

package com.itheima.controller;

import com.itheima.dao.StudentDao;
import com.itheima.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;
import java.util.Scanner;

public class StudentController {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new
                ClassPathXmlApplicationContext("applicationContext-student.xml");
        StudentDao studentDao =
                (StudentDao) applicationContext.getBean("StudentDao");
        System.out.println("欢迎来到学生管理系统");
        System.out.println("请输入用户名:");
        Scanner sca = new Scanner(System.in);
        String name = sca.nextLine();
        System.out.printf("请输入%s的密码:\n", name);
        String password = sca.nextLine();
        boolean flag = true;
        List<Student> student = studentDao.findAllStudent();
        for (Student student1 : student) {
            if (name.equals(student1.getUsername()) & password.equals(student1.getPassword())) {
                System.out.println("用户登录成功!");
                System.out.printf("%s是%s班的", name, student1.getCourse());
                flag = false;
                break;
            }
        }
        if (flag) {
            System.out.println("查无此人!");
        }
    }
}

Week9(SpringMVC入门程序)

完成SpringMVC入门程序:

  • 1、创建项目,注意maven类型
  • 2、更新本地仓库
  • 3、下载tomcat,解压到本地,在eclipse中配置tomcat
  • 4、配置依赖,配置web.xml及spring-mvc.xml
  • 5、写程序
    本次作业需要提交电子版实验报告(文件命名:学号+姓名+1125),交给班长,由班长今天收齐统一交给我,报告题目就是:SpringMVC入门程序,报告中需要有完成及测试的步骤说明和截图。

实验报告。。。。。
具体过程就不发了,要的话私聊吧

Week10

最后一个课设,这学期也就草草结束了。

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值