标签:数据 character default this 地址 ack sources char integer
1.需要导入的jar包
1)mybatis核心包
2)mybatis依赖包
3)数据库驱动包
如下图所示 :
2.配置文件:
SqlMapConfig.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatiis.org//DTD Config 3.0//EN" 4 "http://mybatis.org//dtd/mybatis-3-config.dtd"> 5 6 <configuration> 7 <environments default="development"> 8 <environment id="development"> 9 <transactionManager type="JDBC" /> 10 <dataSource type="POOLED"> 11 <property name="driver" value="com.mysql.jdbc.Driver" /> 12 <property name="url" 13 value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" /> 14 <property name="username" value="root" /> 15 <property name="password" value="root" /> 16 </dataSource> 17 </environment> 18 </environments> 19 <!-- mapper文件位置 如果配对了则按住ctrl点击位置会跳跃到文件的位置--> 20 <mappers> 21 <mapper resource="SqlMap/User.xml"/> 22 </mappers> 23 </configuration>
User.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatiis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--写sql语句 --> <mapper namespace="test"><!-- 命名空间为了防止select的id相同时不知道用哪个,但是如果两个xml里面的命名空间不同,那么就可以分辨出来,用命名空间.id --> <!-- 通过id查询用户 如果有多个select需要用id来区分,用parameterType来设置占位符的格式,resultType来设置返回类型,如果是自动返回,则pojo与数据库里面的表一一对应, mybatis占位符#{}--> <select id="findUserById" parameterType="Integer" resultType="com.me.mybatis.pojo.User"> select * from user where id = #{v} </select> </mapper>
测试代码:
1 package com.me.mybatis.junit; 2 3 import java.io.InputStream; 4 5 import org.apache.ibatis.io.Resources; 6 import org.apache.ibatis.session.SqlSession; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 import org.junit.jupiter.api.Test; 10 11 import com.me.mybatis.pojo.User; 12 13 public class MybatisFirstTest { 14 15 @Test 16 void testMybatis() throws Exception { 17 //加载核心配置文件 18 String resource ="SqlMapConfig.xml"; 19 InputStream resourceAsStream = Resources.getResourceAsStream(resource); 20 //创建sqlsessionFactory 21 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); 22 //创建sqlsession 23 SqlSession openSession = sqlSessionFactory.openSession(); 24 //执行sql语句 25 User user = openSession.selectOne("test.findUserById", 10); 26 //输出user 27 System.out.println(user); 28 } 29 }
pojo:
package com.me.mybatis.pojo; import java.io.Serializable; import java.util.Date; public class User implements Serializable { /** * */ private static final long serialVersionUID = 1L; private int id;//id private String username;// 用户姓名 private String sex;// 性别 private Date birthday;// 生日 private String 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 getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address + "]"; } }
项目结构:
结果:
感悟:自己在写的时候由于没有xml文件的头,所以都是自己手动敲得,刚开始敲完了也有错,最后改了xml文件才好的,这些警告一直都有,我换了jar包什么的还是错,但是没有去搜索,如果哪位大神明白,希望可以告诉我一下。另外,包的导入一定不要出错,否则只能提醒不出来你想要的方法,然后你只能去一一排查问题,直到最后才知道原来是这样,有一种恍然大悟的感觉,但是已经浪费了很多时间。
标签:数据 character default this 地址 ack sources char integer
原文地址:https://www.cnblogs.com/tkg1314/p/12271275.html