IDE:intellij
构建工具:gradle
框架:mybatis
配置好后的结构如图:
- 首先添加mybatis和数据库连接jar包
build.gradle
group 'com.study.mybatis'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'war'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/org.mybatis/mybatis
compile group: 'org.mybatis', name: 'mybatis', version: '3.4.4'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.43'
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
- 在数据库中添加几条数据
3.配置连接
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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/studymybatis?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/BlogMapper.xml"/>
</mappers>
</configuration>
- 代码部分
Blog.java
package com.study.mybatis.model;
/**
* Created by on 2017/8/9.
*/
public class Blog {
int id;
String author;
@Override
public String toString() {
return "Blog{" +
"id=" + id +
", author='" + author + '\'' +
'}';
}
}
BlogMapper.java
package com.study.mybatis.mapper;
import com.study.mybatis.model.Blog;
/**
* Created by liuqun on 2017/8/9.
*/
public interface BlogMapper {
Blog selectBlog(int id);
}
BlogMapper.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="com.study.mybatis.mapper.BlogMapper">
<select id="selectBlog" resultType="com.study.mybatis.model.Blog">
select * from Blog where id = #{id}
</select>
</mapper>
import com.study.mybatis.mapper.BlogMapper;
import com.study.mybatis.model.Blog;
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;
/**
* Created by liuqun on 2017/8/9.
*/
public class Test {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
try {
BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);
System.out.println(blog.toString());
} finally {
session.close();
}
}
}
最后运行结果
Blog{id=101, author='王五'}
- 第二种方式使用注解
BlogMapper.java
package com.study.mybatis.mapper;
import com.study.mybatis.model.Blog;
import org.apache.ibatis.annotations.Select;
/**
* Created by liuqun on 2017/8/9.
*/
public interface BlogMapper {
@Select("SELECT * FROM blog WHERE id = #{id}")
Blog selectBlog(int id);
}
使用代码配置也可以,将BlogMapp.class注册到配置里,运行结果和上面是一样的
PooledDataSource dataSource = new PooledDataSource();
dataSource.setDriver("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/studymybatis?characterEncoding=utf-8");
dataSource.setUsername("root");
dataSource.setPassword("root");
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
SqlSession session = sqlSessionFactory.openSession();
try {
BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);
System.out.println(blog.toString());
} finally {
session.close();
}