Atitit mybatis3 註解模式使用總結 目錄 1. mybatisdemo 1 1.1. /ormMybatis3demo/src/db.properties 1 1.2. /ormMyb
Atitit mybatis3 註解模式使用總結
目錄
- Mybatisdemo
/ormMybatis3demo/libs/mybatis-3.2.0-SNAPSHOT.jar
-
- /ormMybatis3demo/src/db.properties
# Properties file for JDBC configuration
#
# Place this file in the root CLASSPATH
#MySQL Connection Configuration
mysql.driver=org.sqlite.JDBC
mysql.url=jdbc:sqlite:D:\\000000\\mydatabase.sqlite
mysql.username=root
mysql.password=
ds =java\:/comp/env/jndi/wxb_site_newxxx
-
- /ormMybatis3demo/src/pkg1/MapperCls.java
package pkg1;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Delete;
import org
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
public interface MapperCls {
/*
* 這是基於註解的對映方式,實現對資料的增刪改查,將sql語句直接寫在註解的括號中
* 這是一個介面,其不需要類去實現它
* 下邊分別是插入,刪除,修改,查詢一個記錄,查詢所有的記錄
* */
@Select("${sql_intag}")
public List<Map> query(@Param("sql_intag") String sql);
}
-
- /ormMybatis3demo/src/mybatis.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="db.properties"></properties>
<settings>
<!--
<setting name="defaultExecutorType" value="REUSE" />
-->
<setting name="defaultStatementTimeout" value="30000" />
</settings>
<!-- <plugins>
<plugin interceptor="cn.freeteam.util.OffsetLimitInterceptor"> -->
<!-- <property name="dialectClass" value="cn.freeteam.util.SQLServerDialect"/> -->
<!--</plugin>
</plugins> -->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC" ></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}" />
<property name="url" value="${mysql.url}" />
<property name="username" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />
<property name="poolMaximumIdleConnections" value="0" />
<property name="poolMaximumActiveConnections" value="10" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="pkg1.MapperCls"/>
<!--
<mapper resource="cn/freeteam/model/OperlogsMapper.xml"/>
-->
</mappers>
</configuration>
-
- mybatisdemo
package pkg1;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.alibaba.fastjson.JSON;
public class mybatisdemo {
public static void main(String[] args) {
String resource = "mybatis.xml";
// 載入mybatis 的配置檔案(它也載入關聯的對映檔案)
InputStream is = mybatisdemo.class.getClassLoader().getResourceAsStream(resource);
// 構建sqlSession 的工廠
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session=factory.openSession(true);
MapperCls mapper=session.getMapper(MapperCls.class);
@SuppressWarnings("rawtypes")
// List li =mapper.queryall();
List li =mapper.query("select * from tab1");
System.out.println( JSON.toJSONString(li));
session.close();
// = session.selectList(arg0);
}
}
- 錯誤解決
- mybatis The error may involve defaultParameterMap
@Select("select * from tab1")
public List<Map> queryall();
要定義List<Map> ,不要只是list
-
- org.apache.ibatis.reflection.ReflectionException: There is no getter for property named
解決方法:在引數前加@Param標籤
public List<Userinfo> findAll(@Param("sname") String sname);
- ref
Mybatis框架基於註解的方式,實對資料現增刪改查 - G-&-D - 部落格園.html