1. 创建项目 (一般是创建的web项目, 因为实际开发中, web项目占九成九)
2. 既然要链接数据库, 肯定少不了导包 ( 记得把lib 添加为库 )
mysql-connector-java
mybatis
log4j
3. 添加mybatis的配置文件, 我是直接放在src下
注意改成自己的 propertty 里面的链接项 mapper 里面的映射地址
<?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"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/fmg?serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="24118028"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/fmg/mapper/studentMapper.xml"/> </mappers> </configuration>
4. 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="test1"> <select id="selectById" parameterType="java.lang.String" resultType="com.fmg.domain.Student"> select id, name, age from tbl_student where id = #{id} </select> </mapper>
5. log4j的配置文件
# Global logging configuration log4j.rootLogger=DEBUG,stdout # MyBatis logging configuration... log4j.logger.org.mybatis.example.BlogMapper=TRACE # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
6. 实体类
package com.fmg.domain; public class Student { private String id; private String name; private Integer age; public Student() { } public Student(String id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", age=" + age + '}'; } }
7. 测试文件
package com.fmg.test; import com.fmg.domain.Student; 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 java.io.IOException; import java.io.InputStream; public class TestMain1 { public static void main(String[] args) { String resource = "mybatis-config.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { e.printStackTrace(); } SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); Student stu = session.selectOne("test1.selectById", "A0001"); System.out.println(stu); } }
8. 项目结构
10. 运行结果