发表于: 2017-12-18 17:03:03
2 603
今天完成的事情:使用mybatis连接数据库,但是还是没有读取到数据
User.java
package mybatis.po;
import java.util.Date;
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public User(String username, Date birthday, String sex, String address) {
this.username = username;
this.birthday = birthday;
this.sex = sex;
this.address = address;
}
}
mybatisFirst.java
package mybatis.first;
import mybatis.po.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.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
public class MybatisFirst {
public SqlSession getSession() throws IOException {
String resource = "SqlMapConfig.xml"; //mybatis配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
@Test
public void findUserById() throws IOException {
SqlSession sqlSession = getSession(); //调用上面的方法获取sqlSession
User user = sqlSession.selectOne("test.findUserById", 1);
System.out.println(user);
sqlSession.close();
}
}
SqlMapConfig.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>
<!-- 和Spring整合后environment配置都会被干掉 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理,目前由mybatis来管理 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池,目前由mybatis来管理 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="cao562029220" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="sqlmap/User.xml" />
</mappers>
</configuration>
User.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="test">
<!-- 需求:通过id查询用户 -->
<select id="findUserByName" parameterType="java.lang.String" resultType="mybatis.po.User">
select * from user where username like '%${value}%'
</select>
</mapper>
红色的那块好像有问题,不知道怎么改
明天计划的事情:继续学习spring这部分的知识。上周事情比较多耽误了很多进度。着手买服务器并准备后面的任务
遇到的问题:
收获:
评论