发表于: 2020-05-01 23:16:36
1 1375
今天做的事情:
4.Interface和ServiceImpl分开
DiscipleDao.java
public interface DiscipleDao {
List<Disciple> query(Disciple disciple);
int insert(Disciple disciple);
int update(Disciple disciple);
int delete(int id);
}
DiscipleDaoImpl.java
public class DiscipleDaoImpl extends SqlSessionDaoSupport implements DiscipleDao {
@Override
public List<Disciple> query(Disciple disciple){
SqlSession sqlSession = this.getSqlSession();
System.out.println("[" + sqlSession + "]");
List<Disciple> discipleList = null;
try {
discipleList = sqlSession.selectList("mapper.DiscipleDao.query", disciple);
}catch (DataAccessException e){
System.out.println(e);
}
return discipleList;
}
@Override
public int insert(Disciple disciple){
SqlSession sqlSession = this.getSqlSession();
int result = -1;
try {
result = sqlSession.insert("mapper.DiscipleDao.insert", disciple);
}catch (DataAccessException e){
System.out.println(e);
}
return result;
}
@Override
public int update(Disciple disciple){
SqlSession sqlSession = this.getSqlSession();
int result = -1;
if (disciple.getId() > 0){
try {
result = sqlSession.update("mapper.DiscipleDao.update", disciple);
}catch (DataAccessException e){
System.out.println(e);
}
}
return result;
}
@Override
public int delete(int id){
SqlSession sqlSession = this.getSqlSession();
int result = -1;
try {
result = sqlSession.delete("mapper.DiscipleDao.delete", id);
}catch (DataAccessException e){
System.out.println(e);
}
return result;
}
}
Service.java
public interface Service {
List<Disciple> query(Disciple disciple);
int insert(Disciple disciple);
boolean update(Disciple disciple);
boolean delete(int id);
String queryForLink(Map<String,String> name_OR_id);
}
ServiceImpl.java
public class ServiceImpl implements Service {
private DiscipleDao discipleDao;
public void setDiscipleDao(DiscipleDao discipleDao){ this.discipleDao = discipleDao; }
public DiscipleDao getDiscipleDao(){ return discipleDao; }
@Override
public List<Disciple> query(Disciple disciple){
return discipleDao.query(disciple);
}
@Override
public int insert(Disciple disciple){ return discipleDao.insert(disciple); }
@Override
public boolean update(Disciple disciple){
return discipleDao.update(disciple) == 1 ? true: false;
}
@Override
public boolean delete(int id){
return discipleDao.delete(id) == 1 ? true: false;
}
@Override
public String queryForLink(Map<String, String> name_OR_id){
Disciple disciple = new Disciple();
if (!name_OR_id.isEmpty()){
disciple.setStudent_name(name_OR_id.get("name"));
if (name_OR_id.containsKey("id")){
int id = 0;
try {
id = Integer.parseInt(name_OR_id.get("id"));
}catch (NumberFormatException e){
return "The value of 'id' is not Integer.";
}
disciple.setStudent_id(id);
}
List<Disciple> discipleList = discipleDao.query(disciple);
if (discipleList.size() == 1){
return discipleList.get(0).getDaily_report();
}
else {
return discipleList.size() == 0 ? "Match Nothing." : "Too many context.";
}
}
else {
return "Empty Map.";
}
}
}
2.除了CRUD的基本单元测试,加上根据学员名字,学号去查找报名贴的单元测试
testQueryForLink.java
public class testQueryForLink {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Service service = (Service)applicationContext.getBean("service");
@Test
public void testQueryForLink(){
Map<String, String> stringStringMap = new HashMap<>();
stringStringMap.put("name", "奇异博士-NO:1");
System.out.println(service.queryForLink(stringStringMap));
stringStringMap.put("id", "5339");
System.out.println(service.queryForLink(stringStringMap));
stringStringMap.replace("name", "奇异博士-NO:2");
System.out.println(service.queryForLink(stringStringMap));
System.out.println(new String("").getClass());
}
}
6.使得Log4j来记录日志
log4j.properties
log4j.rootLogger = DEBUG,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
项目里面用到了,但是这个配置文件的具体意义我不懂,照抄的。
3.Java代码符合命名规范
参照了阿里巴巴Java开发命名规范。
9.访问数据的时候使用Try/Catch捕获异常,关闭DB之后测试异常代码可以正确执行
没有问题的。
10.关闭连接
这个说的应该是关闭数据库的连接吧,因为我用的是 mybatis-spring,会自动关闭连接。
遇到的问题:
1.插入数据后 create_time 字段不是当前时间,是默认的 0000-00-00 00:00:00
之前数据库建表语句中两个字段中都有时间戳就报错,发现是 mysql5.5 这个版本的 bug,修改后虽然建表后不再报错了,但是其中一个字段的时间需要在插入语句中用代码生成,而我的插入语句中没有写上。
解决方法就是在 xml 的插入语句中加上 create_time 字段,在对应的 value 位置加上 now()
2.调用 service 层后报错 sqlSession 为空的问题
这个实际上是我不理解 spring 的注入方式,不懂怎么配置 bean。
解决方式是在 service 里面注入 dao,而不是想着在 service 里面 new 一个 dao,再往 dao 里面注入 sqlSessionFactory。
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
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:db.properties" />
<!--配置c3p0连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置sqlSqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入mybatis核心配置文件-->
<property name="configLocation" value="mybatis-config.xml"></property>
<!--注入数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--给 dao 注入上面配置好的 sqlSqlSessionFactory-->
<bean id="discipleDao" class="cn.mogeek.dao.DiscipleDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!--给 service 注入上面配置好的 dao-->
<bean id="service" class="cn.mogeek.service.ServiceImpl">
<property name="discipleDao" ref="discipleDao"></property>
</bean>
</beans>
被注入的类对应的变量要有 set 方法:
DiscipleDaoImpl.java
public class ServiceImpl implements Service {
private DiscipleDao discipleDao;
public void setDiscipleDao(DiscipleDao discipleDao){ this.discipleDao = discipleDao; }
/**
````````````
*/
}
昨天配置这个地方卡了很久,然后配置文件也是网上找的,出现了不少错误。
7.通过远程连接Mysql,使用自定义域名并通过配置本地Host来配置DB连接文件
(a)使用管理员权限打开 CMD,输入 notepad,从记事本中打开 host 文件,添加记录:
xxx.xx.xx.xxx www.mogeek.cn
(b)cmd 输入 ipconfig/flushdns 刷新 host
(c)在 db 配置文件中更改 jdbc.url
测试:
明天的计划:
1.使用注解配置 mybatis 数据库(mybatis-spring 应该不能这么配置数据库吧,明天去新建一个 mybatis 项目完成这个步骤)
2.开始深度思考
评论