两种数据库连接⽅式两种注解(注解版urmapper,⽂件
版)
⼀、两种连接数据库的⽅式
⽅式⼀:
springboot默认配置了(DataSource和sqlssionfactory)
直接配置⽂件l
⾥⾯写
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/o2odb?rverTimezone=UTC&characterEncoding=utf-8&uSSL=true
driver-class-name: sql.jdbc.Driver
姚明扣篮
urname: root
password: root
⽅式⼆:
类似springmvc 配置datasource和 ssionfactory,然后 写⼊⼀个bean加⼊到容器。
application.properties⾥⾯如下
jdbc.sql.jdbc.Driver
jdbc.url= jdbc:mysql://localhost:3306/o2odb?uUnicode=true&characterEncoding=utf8
jdbc.urname=root
jdbc.password=root
然后新建两个config
hange.v2.c3p0.ComboPooledDataSource;
batis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import t.annotation.Bean;
import t.annotation.Configuration;
import java.beans.PropertyVetoException;
@Configuration
@MapperScan("dao")
public class DataSourceConfiguration {
@Value("${jdbc.driver}")
private String jdbcDriver;
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.urname}")
private String jdbcUrname;
@Value("${jdbc.password}")
private String jdbcPassword;
@Bean(name = "dataSource")
public ComboPooledDataSource createDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//dirver
dataSource.tDriverClass(jdbcDriver);
//url
slap
dataSource.tJdbcUrl(jdbcUrl);
//urname
dataSource.tUr(jdbcUrname);
//password
dataSource.tPassword(jdbcPassword);
//在关闭连接之后不会⾃动的Commit
dataSource.tAutoCommitOnClo(fal);
// <!--当连接池中的连接耗尽的时候c3p0⼀次同时获取的连接数。Default: 3 -->
dataSource.tAcquireIncrement(5);
// <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
dataSource.tAcquireRetryAttempts(30);
/
/ <!--两次连接中间隔时间,单位毫秒。Default: 1000 -->
dataSource.tAcquireRetryDelay(1000);
// <!-- 当连接池⽤完时客户端调⽤getConnection()后等待获取新连接的时间,超时后将抛出SQLException dataSource.tCheckoutTimeout(10000);
dataSource.tMaxStatements(0);
//<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。 Default: 3 -->
dataSource.tInitialPoolSize(10);
dataSource.tMinPoolSize(5);
// <!--连接池中保留的最⼤连接数。Default: 15 -->
dataSource.tMaxPoolSize(200);
// <!--最⼤空闲时间,60秒内未使⽤则连接被丢弃。若为0则永不丢弃。Default: 0 -->
dataSource.tMaxIdleTime(60);
//<!--How long to hang on to excess unud connections after traffic spike -->
dataSource.tMaxIdleTimeExcessConnections(600);
return dataSource;
}
}
batis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import t.annotation.Bean;
import t.annotation.Configuration;
import io.ClassPathResource;
import io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
import java.io.IOException;
@Configuration
public class SessionFactoryConfiguration {
@Autowired
private DataSource dataSource;
@Bean(name="sqlSessionFactory")
public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException
{
谷歌中译英在线翻译SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//加载主配置⽂件l
bean.tConfigLocation(new ClassPathResource("l"));
//Mapper扫描路径
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); String packageSearchPath = "classpath*:/mapper/**.xml";
bean.Resources(packageSearchPath));
//配置实体的包
阅读bean.tTypeAliasPackage("com.del");
//dataSource
bean.tDataSource(dataSource);
return bean;
}
}
启动即可。
⼆、两种和数据库打交道
@RestController
public class wenjianversioncontroller {
@Autowired
tomato怎么读
private UrDao urDao;
@RequestMapping("/wenjianfindall")
public String wenjianfindall(){
List<Ur> urs = urDao.archAllUrs();
for (Ur ur : urs) {
System.out.Id()+"--"+ur.getUrname()+""+ur.getPassword());
}
urDao.deleteUrById(2);
return "wenjianfindall";
}
public interface UrDao {
//增
int addUr(Ur ur);
//删
//int deletUr(Ur ur);
int deleteUrById(int id);
//改
int updateUr(Ur ur);
//查
mikey什么意思
List<Ur> archAllUrs();
Ur archUrById(int id);
}
然后resource⾥⾯的mapper⾥⾯
advocates<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<!-- namespace:Mapper对应的DAO -->
<mapper namespace="com.hy.oa.dao.UrDao">
<!-- lect:查询⼦句
id:对应的⽅法名称
resultType: 返回的类型-->
<lect id="archAllUrs" resultType="com.del.Ur">
lect * from ur
</lect>
<lect id="archUrById" resultType="com.del.Ur">
lect * from ur where id = #{id}
</lect>
<!-- urGeneratedKeys:如果执⾏成功,则返回主键的值,parameterType:⼊参的类
<inrt id="addUr1" uGeneratedKeys="true">
inrt into ur(urname,password) values(#{urname},#{password})
lf
</inrt>-->
<inrt id="addUr" uGeneratedKeys="true" keyProperty="id" keyColumn="id" parameterType="com.del.Ur"> inrt into ur values(#{id},#{urname},#{password})
</inrt>
<update id="updateUr" parameterType="com.del.Ur">
update ur t urname=#{urname},password=#{password} where id=#{id}
</update>
<delete id="deleteUrById">
delete from ur where id = #{id}
</delete>
<!--<delete id="deleteUrById2">
delete from ur where id = #{Lain}
</delete>-->
</mapper>
----------------------------------crazy hor
⽅式⼆:
dao⾥⾯直接注解,然后注解⾥⾯写sql语句。
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UrMapper {
@Select("lect * from ur")
List<Ur> findAll();
fpa}