发表于: 2017-05-07 22:13:16
0 3485
今日目标:对任务1进行收尾,完成除了将数据库部署到远程服务器之外的步骤(条件有限)
1.添加Mybatis,并且实现使用xml方法和注解方法。
2.将DAO修改为添加数据返回ID,删除或更新数据返回True/False。
3.添加c3p0数据库连接池。
//前几天断网没更新日志……
步骤1:
1.在src/main/java目录下建立mybatis-config.xml,规定xml格式并且加入Mybatis规定的DTD。
<?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">
2.根据DTD的指导(一路alt+/),写好需要的内容。指定mapper配置文件的目录。
一开始这里的驱动包没有大写,而且包名还拼错,导致了java.lang.ClassNotFoundException: Cannot find class: com.jdbc.mysql.driver错误。
<?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>
<typeAliases>
<package name="com.qhs.DB_crud.bean"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/学员报名?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/qhs/DB_crud/bean/student.xml"/>
</mappers>
</configuration>
3.新建com/qhs/DB_crud/bean/student.xml,写好DTD
<?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">
4.以及指向signup表,以及指向student的配置
<mapper namespace="com.qhs.DB_crud.bean">
<select id="listStudent" resultType="Student">
select * from signup
</select>
</mapper>
5.最后编写一个测试类,测试Mybatis的配置。
在编写测试类的时候遇到问题:之前使用jUnit的时候,编写的方法有布尔类返回值,所以当时不求甚解的调用了assertTrue()方法。
之前找到的教程导包和语法不太一样,直接搜索导包语句,找到一篇比较浅显的:http://zhangjunhd.blog.51cto.com/113473/132553/
对测试方法进行修改:
当抛出异常的时候
public void testApp()
{
try {
new Main().query();
} catch (IOException e) {
}
}
题外话:我尝试关闭Mysql服务,让程序产生异常,执行net stop MySQL的时候报了没有权限的错误。
懒得用Win+x开启管理员下的cmd,搜索了一下,Windows 下的CMD有runas命令,作用类似Linux下的sudo:
C:\Users\QHS>runas /user:Administrator "cmd net stop Mysql"
输入 Administrator 的密码:
试图将 cmd net stop Mysql 作为用户 "QHS-PC\Administrator" 启动...
RUNAS 错误: 无法运行 - cmd net stop Mysql
1327: 用户帐户限制阻止了此用户进行登录。例如:不允许使用空密码,登录次数的限制,
或强制实施的某个策略限制。
不知道Windows怎么做到在同一个QHS用户下设置正常权限和管理员权限的……
main类中的query方法:
public void query() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
listAll(session);
session.commit();
session.close();
}
private static void listAll(SqlSession session) {
List<Student> ss = session.selectList("listStudent");
for (Student s : ss) {
System.out.println(s.getName());
}
}
6.将配置方式改为注解。
新建包com.qhs.DB_crud.mapper以及接口StudentMapper。
并且将sql语句以及具体的操作写成注释。
package com.qhs.DB_crud.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Select;
import com.qhs.DB_crud.bean.Student;
public interface StudentMapper {
@Select("select * from signup")
public List<Student> list();
}
在mybatis-config.xml中,添加一个mapper:
<mapper class="com.qhs.DB_crud.mapper.StudentMapper"/>
在主方法中,获取一个mapper:
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
//listAll(session);
//getStudent(mapper);
session.commit();
session.close();
}
至此步骤1全部完成。
步骤2:对DAO进行修改。
1.在接口中修改add方法的返回值。(update.delete方法已经写成返回boolean)
public int add(Student student);
2.修改StudentDAO类中的具体实现:
if(rs.next()){
//此时rs如果不为空,则只含有自增id
int i = rs.getInt(1);
return i;
}
步骤3:添加c3p0数据库连接池。
1.在Maven中添加c3p0。
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
2.在mybatis-config.xml中将数据源改为c3p0
<dataSource type="org.mybatis.c3p0.C3P0DataSourceFactory">
2.一开始以为只需要改数据源。。然后发现整个dataSource都要改……
<dataSource type="org.mybatis.c3p0.C3P0DataSourceFactory">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/学员报名?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false"/>
<property name="user" value="root" />
<property name="password" value="admin" />
<!-- 连接池初始化大小为3 -->
<property name="initialPoolSize" value="3"/>
<!-- 连接池最大为10 -->
<property name="maxPoolSize" value="10"/>
<!-- 连接池最小为3 -->
<property name="minPoolSize" value="3"/>
<!-- 连接池在无空闲连接可用时一次性最多创建的新数据库连接数 -->
<property name="acquireIncrement" value="5"/>
<!-- 连接的最大空闲时间,如果超过这个时间(秒),某个数据库连接还没有被使用,则会断开掉这个连接。如果为0,则永远不会断开连接,即回收此连接 -->
<property name="maxIdleTime" value="30"/>
<!-- 最大的Statement数量 -->
<property name="maxStatements" value="500"/>
<!-- 每个连接启动的最大Statement数量 -->
<property name="maxStatementsPerConnection" value="50"/>
<!-- 同时运行的线程数 -->
<property name="numHelperThreads" value="5"/>
</dataSource>
番外:参考http://blog.csdn.net/csu_max/article/details/40555819,对Eclipse和Github进行了连接。
明日目标:学习SpringMVC,并且尝试任务2
收获:熟悉了Mybatis的xml、注解方式配置,熟悉了对Mybatis和c3p0的整合,将IDE和代码仓库关联。
评论