发表于: 2019-12-24 12:52:04
1 982
今天完成的事情:
1.加过service层的代码
1)目录
2)代码
User
package com.wp.sm.beans;
public class User {
private int id;
private String name;
private String pwd;
public User(){};
public User(String name,String pwd){
this.name=name;
this.pwd=pwd;
}
public User(String name,String pwd,int id){
this.name=name;
this.pwd=pwd;
this.id=id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
}
}
UserMapper
package com.wp.sm.mapper;
import com.wp.sm.beans.User;
import java.util.List;
public interface UserMapper {
public int insertUser(User user) throws Exception;
public int updateUser(User user) throws Exception;
public int deleteUser(int id) throws Exception;
public User selectUserById(int id) throws Exception;
public List<User> selectAllUser() throws Exception;
}
UserMapper.xml
<mapper namespace="test">
<insert id="insertUser" parameterType="com.wp.sm.beans.User">
insert into user (name,pwd)
values (#{name}, #{pwd})
</insert>
<update id="updateUser" parameterType="com.wp.sm.beans.User">
update user
set name=#{name},
pwd=#{pwd}
where id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
delete
from user
where id = #{id}
</delete>
<select id="selectUserById" parameterType="int" resultType="com.wp.sm.beans.User">
select *
from user
where id = #{id}
</select>
<select id="selectAllUser" parameterType="com.wp.sm.beans.User" resultType="com.wp.sm.beans.User">
select *
from user
</select>
</mapper>
UserService
package com.wp.sm.service;
import com.wp.sm.beans.User;
import java.util.List;
public interface UserService {
public int insertUser(User user) throws Exception;
public int updateUser(User user) throws Exception;
public int deleteUser(int id) throws Exception;
public User selectUserById(int id) throws Exception;
public List<User> selectAllUser() throws Exception;
}
UserServiceImpl
package com.wp.sm.service.impl;
import com.wp.sm.beans.User;
import com.wp.sm.service.UserService;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
public class UserServiceImpl extends SqlSessionDaoSupport implements UserService {
@Override
public int insertUser(User user) throws Exception {
SqlSession sqlSession = this.getSqlSession();
int insert = sqlSession.insert("test.insertUser", user);
return insert;
}
@Override
public int updateUser(User user) throws Exception {
SqlSession sqlSession = this.getSqlSession();
int update = sqlSession.update("test.updateUser", user);
return update;
}
@Override
public int deleteUser(int id) throws Exception {
SqlSession sqlSession = this.getSqlSession();
int del = sqlSession.delete("test.deleteUser", id);
return del;
}
@Override
public User selectUserById(int id) throws Exception {
SqlSession sqlSession = this.getSqlSession();
User user = sqlSession.selectOne("test.selectUserById", id);
return user;
}
@Override
public List<User> selectAllUser() throws Exception {
SqlSession sqlSession = this.getSqlSession();
List<User> user = sqlSession.selectList("test.selectAllUser");
return user;
}
}
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:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"/>
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载 MyBatis 的配置文件 -->
<property name="configLocation" value="mybatis.xml"/>
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="UserServiceImpl" class="com.wp.sm.service.impl.UserServiceImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
mybatis.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>
<!--mybatis.xml里注册UserMapper.xml文件-->
<mappers>
<mapper resource="com/wp/sm/mapper/UserMapper.xml"/>
</mappers>
</configuration>
db.properties
username=root
password=19980710
url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
driver=com.mysql.cj.jdbc.Driver
UserServiceTest
import com.wp.sm.beans.User;
import com.wp.sm.service.UserService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class UserServiceTest {
private ApplicationContext applicationContext;
// 在执行测试方法之前首先获取 Spring 配置文件对象
// 注解@Before在执行本类所有测试方法之前先调用这个方法
@Before
public void setup() throws Exception {
applicationContext = new
ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
@Test
public void selectUserById() throws Exception {
// 通过配置资源对象获取 userService 对象
UserService userService = (UserService) applicationContext.getBean("UserServiceImpl");
// 调用 UserMapper 的方法
User user = userService.selectUserById(1);
// 输出用户信息
System.out.println(user.toString());
}
@Test
public void selectAllUser() throws Exception {
// 通过配置资源对象获取 userService 对象
UserService userService = (UserService) applicationContext.getBean("UserServiceImpl");
// 调用 UserMapper 的方法
List<User> user = userService.selectAllUser();
// 输出用户信息
System.out.println(user.toString());
}
@Test
public void deleteUser() throws Exception {
// 通过配置资源对象获取 userService 对象
UserService userService = (UserService) applicationContext.getBean("UserServiceImpl");
userService.deleteUser(10);
}
@Test
public void updateUser() throws Exception {
// 通过配置资源对象获取 userService 对象
UserService userService = (UserService) applicationContext.getBean("UserServiceImpl");
User user = new User();
user.setName("万万");
user.setPwd("111111");
user.setId(3);
userService.updateUser(user);
}
@Test
public void insertUser() throws Exception {
// 通过配置资源对象获取 userService 对象
UserService userService = (UserService) applicationContext.getBean("UserServiceImpl");
User user = new User();
user.setName("peter");
user.setPwd("111111");
userService.insertUser(user);
}
}
2.注解的方法
在service层的实现类加了@Service注解
@Service
public class UserServiceImpl extends SqlSessionDaoSupport implements UserService
在spring的配置文件中加了bean
<bean id="UserServiceImpl" class="com.wp.sm.service.impl.UserServiceImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
使用注解之后的实现类
import com.wp.sm.beans.User;
import com.wp.sm.service.UserService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class UserServiceTest {
@Autowired
UserService userService;
@Test
public void selectUserById() throws Exception {
User user = userService.selectUserById(1);
// 输出用户信息
System.out.println(user.toString());
}
@Test
public void selectAllUser() throws Exception {
List<User> user = userService.selectAllUser();
// 输出用户信息
System.out.println(user.toString());
}
@Test
public void deleteUser() throws Exception {
userService.deleteUser(10);
}
@Test
public void updateUser() throws Exception {
User user = new User();
user.setName("万万");
user.setPwd("111111");
user.setId(3);
userService.updateUser(user);
}
@Test
public void insertUser() throws Exception {
User user = new User();
user.setName("peter");
user.setPwd("111111");
userService.insertUser(user);
}
}
明天计划的事情: 再好好学一下注解的使用和log4j,修改代码
遇到的问题: 对注解的理解还是不足
收获:
多态性
1.理解多态性:可以理解为一个事物的多种形态
2.何为多态性
对象的多态性:父类的引用指向子类的对象(或者子类的对象赋给父类的引用)
例如:Person p1 = new Man ( ) ;
3.多态的使用:当调用子父类同名同参数的方法时,实际执行的是子类重写父类的方法---虚拟方法调用。编译期,调用父类声明的方法。执行期,执行子类重写父类的方法。
4.多态性的使用前提:类的继承关系,方法的重写。
评论