心存理想,梦会实现
该篇技术性知识很少,主要感悟和理解
未来
一个没有六级只有四级的大四本科学生,招聘会也去过两次,也确实自己没有六级什么的不会优先考虑,只能去学习技能,从最开始看到哪个不会学哪个的不规则学习,到现在系统的看书学习
每天只有一种感觉,学过的又忘了
到现在也错过了最佳的秋招时期
只想每天忙忙碌碌充实一点
头发多头发少,一模掉一把
头发只是开个玩笑,这几天在学习MyBatis,为什么学习呢,因为一本书项目从入门到实战,
开篇就讲mybatis,只能硬着头皮上,从上一次到现在也算是刚刚搞清楚大体的东西,今天也是第一次运行成功。
只是代码搬运工
运行成功也是东copy,西copy,在百度搜搜,也算是有点成就,不至于无数次失败没有成功
代码最后贴吧,希望可以帮助和我一样的初学者
框架文件夹如下:
配置文件如下:
<?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>
<environments default="development">
<environment id="development">
<!-- JDBC管理-->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mysqldb?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="PASSWORD"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
mapper配置文件:
<?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">
<select id='findUserById' parameterType="int" resultType="com.test.pojo.User">
select * from user
</select>
</mapper>
test测试:
package com.test.dao;
import com.test.util.mybatisuti;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.io.IOException;
import com.test.pojo.User;
public class testt {
public mybatisuti mi = new mybatisuti();
@Test
public void TestSelect() throws IOException{
SqlSession sqlSession=mi.getSqlSession();
User user = sqlSession.selectOne("test.findUserById",1);
System.out.println("姓名:"+user.getUsername());
System.out.println("生日:"+user.getBirthday());
System.out.println("性别:"+user.getSex());
System.out.println("地址:"+user.getAddress());
sqlSession.close();
}
}
实体类:
package com.test.pojo;
import java.io.Serializable;
public class User implements Serializable {
private int id;
private String username;
private String birthday;
private String sex;
private String address;
public User(){}
public User(int id, String username, String birthday, String sex, String address) {
super();
this.id = id;
this.username = username;
this.birthday = birthday;
this.sex = sex;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String 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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday='" + birthday + '\'' +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
会话工厂会话:
package com.test.util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
public class mybatisuti {
private String resource = "SqlMapConfig.xml";
private SqlSessionFactory sqlSessionFactory;
private SqlSession sqlSession;
public SqlSession getSqlSession() throws IOException{
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}