首页 > 代码库 > mybatis(入门)
mybatis(入门)
首先创建表:建表Sql文件
CREATE DATABASE IF NOT EXISTS `mybatis` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `mybatis`; -- 导出 表 mybatis.t_user 结构 CREATE TABLE IF NOT EXISTS `t_user` ( `username` varchar(50) DEFAULT NULL, `password` varchar(50) DEFAULT NULL, `nickname` varchar(50) DEFAULT NULL, `type` tinyint(4) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
1、创建mybatis-config.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> <properties resource="jdbc.properties" /> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 配置mybatis的pooled的数据源 --> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="edu/hhxy/btais/User.xml"/><!--加载mapper文件--> </mappers> </configuration>
src/jdbc.properties
2、根据数据表创建相应的实体类
Address.java
package edu.hhxy.btais; public class Address { private int id; private String name; private String phone; private String postcode; private User user; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getPostcode() { return postcode; } public void setPostcode(String postcode) { this.postcode = postcode; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
User.java
package edu.hhxy.btais; import java.util.List; public class User { private int id; private String username; private String password; private String nickname; private int type; private List<Address> addresses; public List<Address> getAddresses() { return addresses; } public void setAddresses(List<Address> addresses) { this.addresses = addresses; } 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 getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public int getType() { return type; } public void setType(int type) { this.type = type; } }
3、创建mapper文件完成对实体类的映射,该文件同样是xml的文件
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="edu.hhxy.btais.User"> <insert id="add" parameterType="edu.hhxy.btais.User"><!--名称空间--> insert into t_user (username,password,nickname,type) value(#{username},#{password},#{nickname},#{type}) </insert> </mapper>
4、创建SQlSession,并且通过SqlSession完成对数据库的操作
package edu.hhxy.btais.test; import java.io.IOException; import java.io.InputStream; 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 edu.hhxy.btais.User; public class TestFirstMybatis { public static void main(String[] args) { try { // 1、创建配置文件(mybatis-config.xml)的输入流 InputStream is = Resources .getResourceAsStream("mybatis-config.xml"); System.out.println(is.available()); // 2、创建SQLSessionFactory SqlSessionFactory factory = new SqlSessionFactoryBuilder() .build(is); // 3、创建SQLSessioin SqlSession session = factory.openSession(); // 4、调用mapper文件插入数据(调用之前需要将mapper文件加入到mybatis-config.xml中) User u = new User(); u.setNickname("孙悟空"); u.setPassword("123"); u.setType(0); u.setUsername("wukong"); session.insert("edu.hhxy.btais.User.add", u); session.commit(); session.close(); } catch (IOException e) { e.printStackTrace(); } } }
5、最佳实践
1、创建相应的MyBatisUtil完成对Session的获取
2、不使用annotation完成sql的映射
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。