sql語句以下:java
create database mybatis; use mybatis; create table user( id int primary key auto_increment , name varchar(50) not null, pwd varchar(50) not null );
插入數據:mysql
insert into user values (1,'daidaimei','12345'),(2,'chenxm','12345'),(5,'dym','11111');
點擊完成。
咱們能夠將mybatis公共的代碼(例如pom.xml文件許多重要配置代碼是重複的)在父項目中完成,這樣下次新建項目的時候只要在父項目的基礎上新建模塊(Module)就好了,就不用每次寫pom.xml文件裏的重複內容;sql
刪除src文件夾;
父項目不寫代碼,只作一些固定的設置,代碼在子項目中完成。數據庫
編寫父項目mybatis的pom.xml文件:mybatis
導入依賴:app
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
*導入資源路徑:maven
<build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
注意Parent繼承父項目mybatis;
點擊完成。ide
在mybatis_01的src>resource文件夾下新建核心配置文件mybatis-config.xml ;工具
<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/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT"/> <property name="username" value="root"/> <property name="password" value="1234"/> </dataSource> </environment> </environments>
注意password填寫本身數據庫的密碼!測試
編寫util、dao、pojo層代碼;
Util工具類:
public class MybatisUtil { private static SqlSessionFactory sqlSessionFactory; static{ try { //使用mybatis獲取sqlSession對象 String resource = "mybatis-config.xml"; InputStream inputStream = null; inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); }catch (IOException e) { e.printStackTrace(); } } //既然有了 SqlSessionFactory,顧名思義,咱們能夠從中得到 SqlSession 的實例。 // SqlSession 提供了在數據庫執行 SQL 命令所需的全部方法。 public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
User實體類:
public class User { private int id; private String name; private String pwd; public User(int id,String name,String pwd) { this.id = id; this.name = name; this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } 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 getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
UserMapper接口:
public interface UserMapper { //查詢所有結果 List<User> GetUserList(); }
UseMapper.xml配置文件(每個Mapper接口類都須要一個同名的xml配置文件來實現它):
<mapper namespace="com.cxm.dao.UserMapper"> <select id="GetUserList" resultType="com.cxm.pojo.User" parameterType="int"> select * from mybatis.user </select> </mapper>
在覈心配置文件mybatis-config.xml中添加mapper聲明:
<mappers> <mapper resource="com/cxm/dao/UserMapper.xml"/> </mappers>
每寫一個接口配置文件(如UserMapper.xml)都須要在覈心配置文件mybatis-config中聲明。
即mybatis-config完整代碼以下:
public class UserDaoTest { @Test public void TestSelect(){ //得到Session對象 SqlSession sqlSession =MybatisUtil.getSqlSession(); //getMapper UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.GetUserList(); //遍歷獲得結果 for(User user : userList){ System.out.println(user); } //關閉sqlSesssion sqlSession.close(); } }
控制檯輸出:
緣由:未在pom.xml中導入資源路徑。
解析:若未在pom.xml中導入build資源路徑,系統不能識別到咱們的xml文件,還有其餘形式的配置文件好比properties配置文件;
解決:把java文件夾和resources文件夾下的xml文件、properties文件聲明爲資源文件。
代碼以下:(只需在父項目的xml文件中寫一遍便可,在父項目的基礎上新建的項目都不用寫)
<build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
注:每次修改pom.xml文件後需點擊更新maven
2. 核心配置文件中亂碼問題
緣由:在mybatis-config.xml文件中寫中文註釋,字符串解析時出問題;
解析:核心配置文件mybatis -config.xml文件和各個接口配置文件UserMapper.xml中都儘可能不要有多餘的註釋、字符,格式儘可能標準、統一。有的IDE可能沒有亂碼問題,有的很嚴格。
解決:檢查、刪除xml文件中的多餘字符、註釋。