发表于: 2018-03-30 22:39:15
1 614
今天完成的事情:
1.重写了一遍基础的jdbc,感觉都忘了
2.使用JdbcTemplate连接远程数据库
3.配置并使用Spring-mybatis连接
白天没写,晚上回来才开始写。
之前的基础根本不熟练,稍微一耽搁就忘,晚上赶紧把能写的都回顾一遍。
1.jdbc基础
1.1)先创建Connection
1.2)使用数据库驱动,获取连接资源
conn = DriverManager.getConnection(url,user,password);
1.3)通过Connection创建Statement接口来使用SQL语句查询
Statement statement = conn.createStatement();
1.4)查询返回数据使用ResultSet承载
ResultSet resultSet = preparedStatement.executeQuery(sql);
1.5)读取ResuSet
2.JdbcTemplate
2.1)导入需要的spring包,配置springxml文件:
先配置Resource资源(jdbc的或者使用数据池)——>
再配置JdbaTemplate,把resource注入到它的属性中(<property>标签ref属性)——>
<bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
再配置业务层(写好业务层),把JdbcTemplate注入到它的属性中——>
<bean id="personService" class="com.jdbctemplate.daoservice.PersonDaoImpl">
<property name="jdbctemplate" ref="jdbctemplate"/>
<!--<constructor-arg name="jdbctemplate" value="jdbctemplate"/>-->
</bean>
2.2)创建实体类,创建Dao接口和它的实例化对象(业务层):
业务层写具体查询方法,并且根据配置的需要选择set或者构造注入去获得JdbcTemplate的bean(IOC和DI的概念)
2.3)单元测试:
先获取xml配置——>
ApplicationContext conn = new ClassPathXmlApplicationContext("jdbctemplate/applicationcontext.xml");
再通过配置连接获取到业务层的Bean(通过强制转换)——>
PersonDaoImpl personimpl = (PersonDaoImpl) conn.getBean("personService");
然后就可以通过业务层的方法来操作数据库——>
System.out.print(personimpl.selectById(1));
3.spring-mybatis配置和连接
3.1)pom导入mybatis-spring相关jar包
<!--mybatis包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--mybati-spring包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
3.2)配置xml文件,配置mapper文件
创建Mybatis工厂bean,并再属性中注入mapper.xml的扫描路径——>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
创建MapperScannerConfigurer,并注入把Dao和sqlSession工厂bean注入其中(它可以直接实例化接口,不需要我们再实例化)——>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactiory"/>
<property name="basePackage" value="mybatis.mapper"/>
</bean>
3.3)创建Mapper类和实体类
在接口里写方法,mybatis会自动创建实例。
3.4)单元测试:
获取配置——>
Reader reader = Resources.getResourceAsReader("mybatis/spring-mybatis.xml");
// InputStream inputStream = TestMybatis.class.getClassLoader().getResourceAsStream("mybatis/spring-mybatis.xml");
创建Session——>
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlSessionFactory.openSession();
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
获取数据——>
System.out.print(personMapper.selectById(1).toString());
太困了,睡觉。
明天计划的事情:
往下继续是——使用注解配置一遍——写完整,然后打包——上传服务器——在服务器跑测试
遇到的问题:
org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 71; 文档根元素 "beans" 必须匹配 DOCTYPE 根 "null"。
没找到原因,标签齐全,xml头没错,配置也没错。明天继续找
收获:
…加深印象,但是写的比较急,精神状态不是很好。稍微一写就到大半夜了,不过一晚上把之前的都走个大概,还好没忘干净。
还是把之前学到的知识大致的捋了一遍,至少jdbctemplate和mybatis和基础的jdbc稍微有些明白区别在哪了,这是核心的东西,还是应该多写记得牢一些。
评论