框架作业0715

作业一
在这里插入图片描述
1.创建项目并导入spring的相关jar包
2.编写pojo包
User.java

package com.openlab.pojo;
public class User {

    private Integer id;
    private String name;
    private String password;
    private String email;
    private String address;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

3.编写dao包
接口UserDao.java

package com.openlab.dao;
import com.openlab.pojo.User;

public interface UserDao {

    int addUser(User user);
}

实现类UserDaoImpl.java

package com.openlab.dao.impl;
import com.openlab.dao.UserDao;;
import com.openlab.pojo.User;

public class UserDaoImpl implements UserDao {
    @Override
    public int addUser(User user) {
        System.out.println("向数据库中添加了如下用户:");
        System.out.println(user.getId() + "\t" + user.getName() + "\t" + user.getPassword() + "\t" + user.getEmail() + "\t" + user.getAddress());
        System.out.println("添加成功");
        return 1;
    }
}

4.编写service包
接口UserService.java

package com.openlab.service;

import com.openlab.pojo.User;

public interface UserService {

    int createUser(User user);
}

实现类UserServiceImpl.java

package com.openlab.service.impl;
import com.openlab.dao.UserDao;
import com.openlab.pojo.User;
import com.openlab.service.UserService;

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

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

    @Override
    public int createUser(User user) {
        int i = userDao.addUser(user);
        return i;
    }
}

5.编写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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.2.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

    <bean id="userDao" class="com.openlab.dao.impl.UserDaoImpl" />

    <bean id="userService" class="com.openlab.service.impl.UserServiceImpl" p:userDao-ref="userDao" />

    <bean id="user" class="com.openlab.pojo.User" p:id="1" p:name="zhangsan" p:password="123456" p:address="中国" p:email="123@aa.com"/>

</beans>

6.编写测试类

package com.openlab.test;

import com.openlab.pojo.User;
import com.openlab.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test01 {

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) ac.getBean("userService");
        User user = (User) ac.getBean("user");
        userService.createUser(user);
    }
}

作业二
在这里插入图片描述

1.创建项目并导入spring的相关jar包以及日志的相关jar包
2.编写log4j.properties
3.编写pojo类

User.java

package com.openlab.pojo;

public class User {

    private Integer id;
    private String name;
    private String password;
    private String email;
    private String address;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

4.编写dao包

接口UserDao.java

package com.openlab.dao;
import com.openlab.pojo.User;
public interface UserDao {

    int addUser(User user);
}

实现类UserDaoImpl.java

package com.openlab.dao.impl;
import com.openlab.dao.UserDao;;
import com.openlab.pojo.User;

public class UserDaoImpl implements UserDao {
    @Override
    public int addUser(User user) {
        System.out.println("向数据库中添加了如下用户:");
        System.out.println(user.getId() + "\t" + user.getName() + "\t" + user.getPassword() + "\t" + user.getEmail() + "\t" + user.getAddress());
        System.out.println("添加成功");
        return 1;
    }
}

5.编写service包
接口UserService.java

package com.openlab.service;
import com.openlab.pojo.User;
public interface UserService {
    int createUser(User user);
}

实现类UserServiceImpl.java

package com.openlab.service.impl;

import com.openlab.dao.UserDao;
import com.openlab.pojo.User;
import com.openlab.service.UserService;

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

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

    @Override
    public int createUser(User user) {
        int i = userDao.addUser(user);
        return i;
    }
}

6.编写logger包,配置相关注解

UserServiceLogger.java

package com.openlab.logger;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import java.util.Arrays;

@Aspect
public class UserServiceLogger {

    private static final Logger log = Logger.getLogger(UserServiceLogger.class);

    @Pointcut("execution(* com.openlab.service.UserService.*(..))")
    public void pointcut(){

    }

    @Before("pointcut()")
    public void beforeLogger(JoinPoint joinPoint){
        log.info("调用了" + joinPoint.getSignature().getName() + "方法,方法的参数是" + Arrays.toString(joinPoint.getArgs()));
    }

    @AfterReturning(returning = "result" , pointcut = "pointcut()")
    public void afterLogger(JoinPoint joinPoint, Object result){
        log.info("调用了" + joinPoint.getSignature().getName() + "方法完成,方法的返回值是" + result);
    }
}

7.配置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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.2.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

    <bean id="userDao" class="com.openlab.dao.impl.UserDaoImpl" />

    <bean id="userService" class="com.openlab.service.impl.UserServiceImpl" p:userDao-ref="userDao" />

    <bean id="user" class="com.openlab.pojo.User" p:id="1" p:name="zhangsan" p:password="123456" p:address="中国" p:email="123@aa.com"/>

    <context:component-scan base-package="com.openlab.*"></context:component-scan>

    <bean id="userServiceLogger" class="com.openlab.logger.UserServiceLogger"/>

    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    
</beans>

8.测试

package com.openlab.test;
import com.openlab.pojo.User;
import com.openlab.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test02 {

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) ac.getBean("userService");
        User user = (User) ac.getBean("user");
        userService.createUser(user);
    }
}

作业三
在这里插入图片描述
1.简述spring和mybatis整合的步骤

(1)创建一个项目,导入spring相关jar包,mybatis的jar包、整合包以及数据库的相关jar包
(2)编写pojo实体类
(3)编写dao包的接口
(4)配置映射文件mapper.xml
(5)编写applicationContext.xml文件,其中添加数据库连接信息,并引入SqlSessionFactory和MapperFactoryBean用作框架整合
(6)测试,获取spring容器,获取dao对象,调用相关方法

2.获取映射接口实例,实现数据操作有哪几种方式
(1)使用MapperFactoryBean注入映射器
(2)使用MapperScannerConfigurer注入映射器

作业四
在这里插入图片描述
1.创建项目并导入spring的相关jar包
2.编写类People.java

package com.openlab.pojo;
public class People {

    private String name;
    private String words;

    public People(){

    }

    public People(String name, String words){
        this.name = name;
        this.words = words;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getWords() {
        return words;
    }

    public void setWords(String words) {
        this.words = words;
    }

    public void talk(){
        System.out.println(name + "说:\""+ words + "\"");
    }
}

3.编写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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.2.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

 <bean id="people1" class="com.openlab.pojo.People">
        <constructor-arg index="0" value="张嘎"></constructor-arg>
        <constructor-arg index="1" value="三天不打小鬼子,手都痒痒!"></constructor-arg>
    </bean>

    <bean id="people2" class="com.openlab.pojo.People">
        <constructor-arg index="0" value="Rom"></constructor-arg>
        <constructor-arg index="1" value="世界上有10种人,认识二进制的和不认识二进制的。"></constructor-arg>
    </bean>
</beans>

4.测试

package com.openlab.test;

import com.openlab.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test03 {

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        People people1 = (People) ac.getBean("people1");
        People people2 = (People) ac.getBean("people2");
        people1.talk();
        people2.talk();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值