标签:-- creates ati 映射 简单 路径加载 serve 复杂 通过
import java.sql.*; public class DatabaseTest { public static void main(String[] args) throws SQLException, ClassNotFoundException { Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8", "root", "123456"); Statement statement = conn.createStatement(); String sql = "select * from user where id = ?"; PreparedStatement preparedStatement = conn.prepareStatement(sql); preparedStatement.setInt(1, 2); ResultSet resultSet = preparedStatement.executeQuery(); // ResultSet resultSet = statement.executeQuery(sql); while (resultSet.next()){ System.out.println(resultSet.getInt(1)); System.out.println(resultSet.getString(2)); System.out.println(resultSet.getString(3)); } } }
直接给你整懵!
个人总结:就是一个端茶送水的店小二。。。将sql语句提交给mysql;并将结果集拿回来。
preparedment是比较高级的店小二,会预编译,而且可以自定义查询值,不像statement每次sql语句都得拼接。
快就完事了preparedment
反正很复杂。
数据可以保持很久,就是保存在磁盘中呗。
完成持久化工作的代码块------->dao(data access object) 数据访问对象
帮助存数据、取数据
优点:简单、灵活、解除sql语句与程序代码的耦合
package com.sicheng.entity; public class User { private int id; private String name; private String 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; } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } public User() { } @Override public String toString() { return "User{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ", pwd=‘" + pwd + ‘\‘‘ + ‘}‘; } }
就是通过各种姿势查找访问数据库的类
例如:通过主键查找、名字查找、、、、、
package com.sicheng.dao; import com.sicheng.entity.User; import java.util.List; public interface UserMapper { List<User> selectUser(); }
应该看的懂吧
<?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="mysql"> <environment id="mysql"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/sicheng/dao/userMapper.xml"/> </mappers> </configuration>
<?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.sicheng.dao.UserMapper"> <select id="selectUser" resultType="com.sicheng.entity.User"> select * from user; </select> </mapper>
用于生产session
package com.sicheng.utils; 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; public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static{ try { String resource = "mybatis-config.xml"; InputStream inputStream = null; inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSession(){ return sqlSessionFactory.openSession(); } }
import com.sicheng.dao.UserMapper; import com.sicheng.entity.User; import com.sicheng.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class MybatisTest { @Test public void selectUser(){ SqlSession session = MybatisUtils.getSession(); UserMapper userMapper = session.getMapper(UserMapper.class); List<User> users = userMapper.selectUser(); for (User user :users) { System.out.println(user); } } }
xml当中不能显示的符号 以及 如何进行转义
< | < | 小于号 |
> | > | 大于号 |
& | & | 和 |
&apos | ‘ | 单引号 |
" | " | 双引号 |
第二种方法:
<![CDATA[ 内部的所有内容都会被解析器忽略 ]]>
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
要写时间区间的,保证安全性?
Resources 类为从类路径中加载资源,提供了易于使用的方法。
所以才在加载时,直接使用mybatis-config.xml这个路径名
Reader getResoutceAsReader(String resource)
Stream getResourceAsStream(String resource)
File getResourceAsFile(String resource)
Properties getResourceAsProperties(String resource);
Url getResourceAsUrl(String resource)
标签:-- creates ati 映射 简单 路径加载 serve 复杂 通过
原文地址:https://www.cnblogs.com/sicheng-li/p/13140705.html