java中使用mybaitis,不依赖任何架构
解决方法:
1.maven依赖:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
2.核心代码
import com.juchn.call.center.dao.entity.DialplanDatailsEntity;
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.InputStream;
class DiaplanDatailsTest {
public static void main(String[] args) throws Exception {
String resource = "mybatis\\mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
DiaplanDetailsMapper mapper = session.getMapper(DiaplanDetailsMapper.class);
DialplanDatailsEntity entity =mapper.select("04c9a55c-ceb0-4d2f-802d-675a4d019229");//DialplanDatailsEntity是你自定义的实体
}
}
3.DialplanDatailsEntity 实体类
public class DialplanDatailsEntity {
private String dialplanDetailUuid;
private String domainUuid;
private String dialplanUuid;
//省略set get
}
4.src\main\resources\mybatis\DiaplanDatailsMapper.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.juchn.call.center.dao.mapper.DiaplanDetailsMapper">
<resultMap type="com.juchn.call.center.dao.entity.DialplanDatailsEntity" id="DialplanDatails">
<id column="dialplan_detail_uuid" property="dialplanDetailUuid"/>
<result column="domain_uuid" property="domainUuid"/>
<result column="dialplan_uuid" property="dialplanUuid"/>
</resultMap>
<select id="select" resultMap="DialplanDatails">
select *
from v_dialplan_details
where dialplan_detail_uuid = #{id}
</select>
</mapper>
5.src\main\resources\mybatis\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>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--本地-->
<property name="url"
value="jdbc:mysql://192.168.0.167:3306/frees?verifyServerCertificate=false&useSSL=false&useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="wlg"/>
<property name="password" value="123456"/>
</properties>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<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="mybatis/DiaplanDatailsMapper.xml"/>
</mappers>
</configuration>