027-leetcode演算法實現之刪除元素-remove-elements-python&golang實現
阿新 • • 發佈:2021-10-10
1,在根目錄的pom.xml中新增maven依賴
<dependencies> <!--mytabis依賴--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!--mysql依賴--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency>
2,建立模組
根目錄->new->module articatid按照規範起名mybatis-01
新增完成後pom.xml配置檔案中會多出modules標籤
3,配置mybatis核心配置檔案(連結資料庫)
mybatis-01->src->main->resources->new file 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"> <transactionManagertype="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <!--填自己的連結資料庫所需要的driver--> <property name="url" value="${url}"/> <!--填寫url--> <property name="username" value="${username}"/> <!--填寫username--> <property name="password" value="${password}"/> <!--填寫password--> </dataSource> </environment> </environments> </configuration>
4,編寫mybatis工具類
//用來獲取sqlsession的工具類 public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { //獲取sqlsessionFactory物件 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
5,資料庫建表,編寫實體類,dao介面,mapper.xml
//實體類
public class user { private int id; private String name; private String pwd; public user() { } public user(int id, String name, String pwd) { this.id = id; this.name = name; this.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; } @Override public String toString() { return "user{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
//dao介面 public interface userDao { public user selectOne(int id);
public List<user> getALl();
}
<!--userMapper.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"> <!--namespace用來繫結dao介面--> <mapper namespace="com.grigeorge.dao.userDao"> <select id="getAll" resultType="com.grigeorge.entity.user"> select * from mybatis.user </select> <select id="selectOne" resultType="com.grigeorge.entity.user"> select * from Blog where id = 1 </select> </mapper>
寫完後須在mybatis-config.xml中新增mapper標籤形成對映關聯
<mappers> <mapper resource="com/grigeorge/dao/userMapper.xml"/> <!--userMapper.xml的相對路徑--> </mappers>
6,測試
public class userDaoTest { public static void main(String[] args) { SqlSession sqlSession = MybatisUtils.getSqlSession(); userDao userDao =sqlSession.getMapper(userDao.class); List<user> userList= userDao.getAll(); for (user user : userList) { System.out.println(user); } // user user = userDao.selectOne(4); // System.out.println(user); } }
補充遇到的一些問題:
###報錯 :Cause: java.io.IOException: Could not find resource com/grigeorge/dao/userMapper.xml
解決方案:在根目錄下的pom.xml中加入以下程式碼
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
###報錯:
解決方案:修改mybatis-config.xml中的url配置,
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
改為:
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"/>
問題解決後得到測試資料