JAVAWEB 30-

快速入门

在这里插入图片描述

public static void main(String[] args) throws Exception {
/1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://127.0.0.1:3306/db1";
String username = "root";
String password = "1234";
Connection conn = DriverManager.getConnection(url, username, password);
//3.定义sql
String sql = "update account set money = 2000 where id = 1";
//4.获取执行sql的对象 Statement
Statement stmt = conn.createStatement();
//5.执行sql
int count =stmt.executeUpdate(sql);//受影响的行数
//6.处理结果
System.out.println();
}

DriverManager

在这里插入图片描述
在这里插入图片描述

Connection

在这里插入图片描述

resultset

在这里插入图片描述

while (rs.next()){

int id = rs.getInt(  1);
String name = rs.getString(  2);
double money = rs.getDouble( 3);
System.out.println(id);
System.out.println(name);
System.out.println(money);
");
System.out.println("----
}

PreparedStatement

在这里插入图片描述

增删改查

public class Brand {
//id主键
private Integer id;/品牌名称
private String brandName;
//企业名称
private String companyName;
// 排序字段
private Integer ordered;/描述信息
private String description;
//状态:8:禁用1:启用
private Integer status;
}

查询所有

//1.获取Connection
//3.加载配置文件
Properties prop = new Properties();
prop.load(new FileInputStream( name:"jdbc-demo/src/druid.properties"));
/4.获取连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5.获取数据库连接 Connection
Connection conn = dataSource.getConnection();
//2.定义SQL
String sql = "select * from tb_brand;";
//3.获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);

//4.设置参数
//5.执行SQL
ResultSet rs = pstmt.executeQuery();
//6.处理结果List<Brand>封装Brand对象,装载List集合
while (rs.next()){
∥获取数据
//封装Brand对象/装载集合
//7.释放资源
rs.close();
pstmt.close();
conn.close();|

添加 修改

//2.定义SQL
String sql = "insert into tb_brand(brand_name, company_name, ordered, description, status
//3.获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
//4.设置参数
pstmt.setString( 1,brandName);
pstmt.setString(2,companyName);
pstmt.setInt( 3,ordered);
pstmt.setString(4,description);
pstmt.setInt( 5,status);
//5.执行SQL
int count=pstmt.executeUpdate();//影响的行数
//6.处理结果
System.out.println(count > p;
//7.释放资源
pstmt.close();
conn.close();

MAVEN坐标

在这里插入图片描述

MyBatis

<dataSource type="POOLED">
<--数据库连接信息-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis?usesSL=false"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
<mapper namespace="test">
<select id="selectAll" resultType="com.itheima.pojo.User">
select * from tb_user;
</select>

</mapper>
//1.加载mybatis的核心配置文件,获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它来执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.执行sql
List<User> users = sqlSession.selectList( "test.selectAll");

System.out.println(users);
<mapper Anamespace="test">
<select id="selectAll" resultType="com.itheima.pojo.User">
select *
from tb_user;
</select>
</mapper>

代理开发

在这里插入图片描述

mybatis查询

在这里插入图片描述

@Test
public void testSelectAll() throws IOException {
//1.获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4.执行方法
List<Brand> brands = brandMapper.selectAll();
System.out.println(brands);
//5.释放资源
sqlSession.close();|

数据库表的字段名称和实体类的属性名称不一样,则不能自动封装数据
*起别名:对不一样的列名起别名,让别名和实体类的属性名一样
*缺点:每次查询都要定义一次别名
*sqL片段
*缺点:不灵活
resultMap:
1.定义标签
2.在标签中,使用resultMap属性替换 resultType属性
id:唯一标识
type:映射的类型,支持别名

在这里插入图片描述

条件查询

在这里插入图片描述

//封装对象
Brand brand = new Brand();
brand.setStatus(status);
brand.setCompanyName(companyName);
brand.setBrandName (brandName);



//执行方法
 
List<Brand> brands = brandMapper.selectByCondition(brand);
System.out.println(brands);

添加

在这里插入图片描述

//执行方法
brandMapper.add(brand);
∥提交事务
sqlSession.commit();|

删除

在这里插入图片描述

参数传递

MyBatis 参数封装:
*单个参数:
1.POJ0类型:直接使用,属性名和参数占位符名称|一致
2.Map集合:
3. Collection:
4. List:
5. Array:
6.其他类型:
多个参数:封装为Map集合,可以使用aParam注解,替换Map集合中默认的arg键名
map.put(“argo”,参数值1)
map.put(“param1”,参数值1)
map.put(“param2”,参数值2)
map.put(“agr1”,参数值2)
–@Param(“username”)
map.put(“username”,参数值1)
map.put(“param1”,参数值1)
map.put(“param2”,参数值2)
map.put(“agr1”,参数值2)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值