最后
现在正是金三银四的春招高潮,前阵子小编一直在搭建自己的网站,并整理了全套的**【一线互联网大厂Java核心面试题库+解析】:包括Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等**
com.alibaba
druid
1.1.0
org.springframework.boot
spring-boot-maven-plugin
在application.yml中添加数据源、Mybatis的实体和配置文件位置。
#默认使用配置
spring:
profiles:
active: dev
#公共配置与profiles选择无关 mapperLocations指的路径是src/main/resources
mybatis:
typeAliasesPackage: com.xdd.entity
mapperLocations: classpath:mapper/*.xml
#开发配置
spring:
profiles: dev
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
就这样就整合完成了!我们测试一下。
用MyBatis Generator自动生成代码,参考博文:http://blog.csdn.net/zhshulin/article/details/23912615 这里列一下自动生成的代码。
import com.xdd.entity.User;
import org.springframework.stereotype.Component;
public interface UserDao {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>id, user_name, password, age
select
from user_t
where id = #{id,jdbcType=INTEGER}
delete from user_t
where id = #{id,jdbcType=INTEGER}
insert into user_t (id, user_name, password,
age)
values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER})
insert into user_t
id,
user_name,
password,
age,
#{id,jdbcType=INTEGER},
#{userName,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
update user_t
user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
where id = #{id,jdbcType=INTEGER}
update user_t
set user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
public class User {
private Integer id;
private String userName;
private String password;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
最后将DemoApplication.java修改一下,让其扫描dao层接口。
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
@MapperScan(“com.xdd.dao”)
public class DemoApplication extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class,args);
}
}
自己添加controller和service
import java.util.List;
import java.util.Map;
public interface UserService {
public User getUserById(int userId);
boolean addUser(User record);
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Service(“userService”)
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
public User getUserById(int userId) {
return userDao.selectByPrimaryKey(userId);
}
public boolean addUser(User record){
boolean result = false;
try {
userDao.insertSelective(record);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
@Controller
@RequestMapping(“/user”)
public class UserController {
@Resource
private UserService userService;
@RequestMapping(“/showUser”)
@ResponseBody
public User toIndex(HttpServletRequest request, Model model){
int userId = Integer.parseInt(request.getParameter(“id”));
User user = this.userService.getUserById(userId);
return user;
}
一线互联网大厂Java核心面试题库
正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!
= Integer.parseInt(request.getParameter(“id”));
User user = this.userService.getUserById(userId);
return user;
}
一线互联网大厂Java核心面试题库
[外链图片转存中…(img-NYghdthy-1715007649579)]
正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!