首页 > 代码库 > mybatis的使用
mybatis的使用
1.导入jar包
asm-3.3.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
log4j-1.2.16.jar
mybatis-3.1.1.jar
mysql-connector-java-5.1.10-bin.jar
slf4j-api-1.6.2.jar
slf4j-log4j12-1.6.2.jar
2.创建一个总的配置文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <typeAliases> 7 <!--给实体类起一个别名 user --> 8 <typeAlias type="com.kite.mybatis.domain.User" alias="User" /> 9 </typeAliases>10 <!-- 配置数据源 -->11 <environments default="development">12 <environment id="development">13 <transactionManager type="jdbc" />14 <dataSource type="POOLED">15 <property name="driver" value="com.mysql.jdbc.Driver" />16 <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />17 <property name="username" value="root" />18 <property name="password" value="aaa123456" />19 </dataSource>20 </environment>21 </environments>22 <!-- 映射文件也是dao的实现类 -->23 <mappers>24 <mapper resource="com/kite/mybatis/domain/UserMapper.xml" />25 </mappers>26 </configuration>
3.创建实体类
1 package com.kite.mybatis.domain; 2 3 public class User 4 { 5 private String id; 6 private String name; 7 private String age; 8 public String getId() 9 {10 return id;11 }12 public void setId(String id)13 {14 this.id = id;15 }16 public String getName()17 {18 return name;19 }20 public void setName(String name)21 {22 this.name = name;23 }24 public String getAge()25 {26 return age;27 }28 public void setAge(String age)29 {30 this.age = age;31 }32 33 }
4.创建dao类
1 package com.kite.mybatis.dao; 2 3 import java.util.List; 4 5 import com.kite.mybatis.domain.User; 6 7 public interface UserMapper 8 { 9 public List<User> selectAll();10 }
5.创建user的映射文件 因为要作为userMapper的实现所以在 文件的头部<mapper namespace="com.kite.mybatis.dao.UserMapper"> 必需与dao类对应
1 <?xml version="1.0" encoding="UTF-8" ?>2 <!DOCTYPE mapper3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">5 <mapper namespace="com.kite.mybatis.dao.UserMapper">6 <select id="selectAll" resultType="user">7 select * from user8 </select>9 </mapper>
6.测试
1 package com.kite.mybatis.test; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;10 11 import com.kite.mybatis.dao.UserMapper;12 import com.kite.mybatis.domain.User;13 14 public class MyBatisTest15 {16 public static void main(String[] args) throws IOException17 {18 //记载mybatis的配置文件19 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("configuraction.xml"));20 //创建打开sesison21 SqlSession session = factory.openSession();22 UserMapper userDao = session.getMapper(UserMapper.class);23 List<User> userList = userDao.selectAll();24 25 System.out.println(userList.size());26 }27 }
一个mybatis的简单测试
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。